IO stream 05 -- video Study Notes for bixiangdong JAVA basic tutorial, 05 -- bixiangdong

Source: Internet
Author: User

IO stream 05 -- video Study Notes for bixiangdong JAVA basic tutorial, 05 -- bixiangdong

Day20

10 create a java file list
11 Properties
12 Properties access
13 Properties
14 Properties exercises
15 PrintWriter
16 merge streams
17. Cut files

 

10 create a java file list

Exercise:
Store the absolute path of a java file under a specified directory to a text file,
Create a java file list file.

Ideas:
1. recursion of the specified directory
2. Obtain the paths of all java files in the recursive process.
3. store these paths in the collection
4. store the data in the set to a file.

1 import java. io. *; 2 import java. util. *; 3 public class JavaFileList 4 {5 public static void main (String [] args) throws IOException 6 {7 File dir = new File ("d: \ abc "); 8 List <File> list = new ArrayList <File> (); 9 fileToList (dir, list); 10 File file = new File (dir, "javafile.txt "); 11 writeToFile (list, file. toString (); 12 13 14} 15 public static void fileToList (File dir, List <File> list) 16 {17 File [] files = Dir. listFiles (); 18 19 for (File file: files) 20 {21 if (file. isDirectory () 22 fileToList (file, list); 23 else24 {25 if (file. getName (). endsWith (". java ") 26 list. add (file); 27} 28} 29} 30 public static void writeToFile (List <File> list, String javaListFile) throws IOException 31 {32 BufferedWriter bufw = null; 33 try34 {35 bufw = new BufferedWriter (new FileWriter (javaListFile); 36 for (File f: list) 37 {38 String pa Th = f. getAbsolutePath (); 39 bufw. write (path); 40 bufw. newLine (); 41 bufw. flush (); 42} 43 44} 45 catch (IOException e) 46 {47 throw e; 48} 49 finally50 {51 try52 {53 if (bufw! = Null) 54 bufw. close (); 55} 56 catch (IOException ie) 57 {58 throw ie; 59} 60} 61} 62}View Code

 

11 Properties

Properties is a subclass of hashtable.
That is to say, it has the characteristics of the map set, and the key-value pairs stored in it are strings,
Is a collection container combined with IO technology.
The features of this object can be used in key-value pairs.

 

Configuration File: when a user logs on to the computer or when the user is using the software, the software system sets and sets the file for the user to load the required environment.

It includes all user-specific configuration settings, such as program items, screen colors, network connections, printer connections, mouse settings, and window size and position.


12 Properties access

String getProperty (String key): Use the specified key to search for properties in this attribute table.

Object setProperty (String key, String value): Call the Hashtable method put.

Set <String> stringPropertyNames (): return the key Set in this attribute class table.

 

1 import java. util. *; 2 public class PropertiesDemo 3 {4 public static void main (String [] args) 5 {6 setAndGet (); 7 8} 9 public static void setAndGet () 10 {11 Properties prop = new Properties (); 12 13 prop. setProperty ("Tina", "22"); 14 prop. setProperty ("Jack", "21"); 15 prop. setProperty ("Marry", "20"); 16 17 String value = prop. getProperty ("Jack"); 18 System. out. println (value); 19 20 Set <String> names = prop. stringPropertyNames (); 21 for (String s: names) 22 {23 System. out. println (s + ":" + prop. getProperty (s); 24} 25} 26}View Code

 

13 Properties

How to store data in a stream to a collection
You want to save the Middle-key data in info.txt to the set for operation.
1. associate a stream with the info.txt file.
2. Read a row of data, split the row with "=", and store the data in the Properties array.

When loading data, the Properties class requires a fixed data format, usually key = value.

1 import java. util. *; 2 import java. io. *; 3 public class PropertiesDemo_2 4 {5 public static void main (String [] args) throws IOException 6 {7 method_1 (); 8 // loadDemo (); 9} 10 public static void method_1 () throws IOException11 {12 BufferedReader bufr = new BufferedReader (new FileReader ("info.txt"); 13 String line = null; 14 Properties prop = new Properties (); 15 while (line = bufr. readLine ())! = Null) 16 {17 String [] arr = line. split ("="); 18 19 prop. setProperty (arr [0], arr [1]); 20} 21 System. out. println (prop); 22 bufr. close (); 23} 24 // use 25 public static void loadDemo () throws IOException26 {27 Properties prop = new Properties () for the load and store methods (); 28 FileInputStream FCM = new FileInputStream ("info.txt"); 29 30 // load the data in the stream into the collection 31 prop. load (FCM); 32 // change the set content 33 prop. setProperty ("Tina", "16"); 34 FileOutputStream fos = New FileOutputStream ("info.txt"); 35 // change the file content, use the store method 36 prop. store (fos, "Hahahaha ~~~ "); 37 38 System. out. println (prop); 39 FCM. close (); 40 fos. close (); 41 42} 43 44}View Code

 

14 Properties exercises

Used to record the number of application programs running,
If the number of times of use has reached, a registration prompt is displayed.

It is easy to think of counters.
However, the counter is defined in the program and exists in the memory as the program runs, and the counter is automatically increased,
However, as the application exits, the counter disappears in the memory.

The next time you start the program, it starts counting again from 0,
This is not what we want.
The value of this counter should exist even if the program ends.
The next time the program starts, it will load the counter and Add 1, and then store it again.

Create a configuration file to record the number of times the software is used.

This configuration file uses a key-value pair to facilitate reading and operating data.
Key-value pairs are map sets,
Data is stored as files, using io technology.
Map + io --> properties

The configuration file can be used to share application data.

The specific implementation code is as follows:

1 import java. io. *; 2 import java. util. *; 3 public class RunCount 4 {5 public static void main (String [] args) throws IOException 6 {7 Properties prop = new Properties (); 8 // encapsulate the File into an object 9 file File = new File ("count. ini "); 10 // if the file does not exist, create a new file 11 if (! File. exists () 12 file. createNewFile (); 13 FileInputStream FCM = new FileInputStream (file); 14 // read the attribute list from the input stream 15 prop. load (FCM); 16 17 // defines the variable, recording the running times 18 int count = 0; 19 String value = prop. getProperty ("time"); 20 21 if (value! = Null) 22 {23 count = Integer. parseInt (value); 24 if (count> = 5) 25 {26 System. out. println ("hello, the usage has arrived. Please register and use it again! "); 27 return; 28} 29} 30 count ++; 31 // change the key value in the Properties table 32 prop. setProperty ("time", count + ""); 33 34 FileOutputStream fos = new FileOutputStream (file); 35 // write key and element pairs in the Properties table to the output stream 36 prop. store (fos, ""); 37 38 // close input/output stream 39 fos. close (); 40. close (); 41 42} 43}View Code

Running status:

When running the sixth time, the console displays:

Hello, the usage has arrived. Please register and use it again!

The content of the count. ini file:

#
# Wed Jan 13 16:33:13 CST 2016
Time = 5

 

15 PrintWriter

Print stream:
The stream provides many printing methods to print all data types as they are.

Byte print stream:
PrintStream
Acceptable parameter types of constructors:
1. File object File
2. String path String
3. byte output stream OutputStream

Character print stream:
PrintWriter
Acceptable parameter types of constructors:
1. File object File
2. String path String
3. byte output stream OutputStream
4. character output stream Writer

1 import java. io. *; 2 public class PrintDemo 3 {4 public static void main (String [] args) throws IOException 5 {6 BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); 7 // automatically refresh 8 // PrintWriter out = new PrintWriter (System. out, true); 9 PrintWriter out = new PrintWriter (new FileWriter ("g.txt"), true); 10 11 String line = null; 12 while (line = bufr. readLine ())! = Null) 13 {14 if ("over ". equals (line) 15 break; 16 // print 17 out with line breaks. println (line. toUpperCase (); 18 // out. flush (); 19 20} 21 out. close (); 22 bufr. close (); 23 24} 25}View Code

 

16 merge streams

SequenceInputStreamIndicates the logical connection of other input streams. It starts from the sorted set of input streams and starts to read from the first input stream,

Until the end of the file is reached, then read from the second input stream, and so on until it reaches the end of the file containing the last input stream.

It can be used to merge files.

1 import java. io. *; 2 import java. util. *; 3 public class SequenceInputStreamDemo 4 {5 public static void main (String [] args) throws IOException 6 {7 Vector <FileInputStream> v = new Vector <FileInputStream> (); 8 9 v. add (new FileInputStream ("d :\\ abc \ 1.txt"); 10 v. add (new FileInputStream ("d :\\ abc \ 2.txt"); 11 v. add (new FileInputStream ("d: \ abc \ 3.txt"); 12 13 Enumeration <FileInputStream> en = v. elements (); 14 15 SequenceInputStream sis = new SequenceInputStream (en); 16 FileOutputStream fos = new FileOutputStream ("d: \ abc \ 4.txt "); 17 18 byte [] buf = new byte [1024]; 19 20 int len = 0; 21 while (len = sis. read (buf ))! =-1) 22 {23 fos. write (buf, 0, len); 24} 25 fos. close (); 26 sis. close (); 27 28 29} 30}View Code

 

17. Cut files

Files can be merged or cut. When the size of a website upload is limited, you need to use file cutting to upload large files.

1 import java. io. *; 2 // cut file 3 public class SplitFile 4 {5 public static void main (String [] args) throws IOException 6 {7 splitFile (); 8 9} 10 public static void splitFile () throws IOException 11 {12 FileInputStream FD = new FileInputStream ("d: \ abc \ bird.jpg"); 13 FileOutputStream fos = null; 14 15 16 byte [] buf = new byte [1024]; 17 int len = 0; 18 int count = 1; 19 while (len = FCM. read (buf ))! =-1) 20 {21 fos = new FileOutputStream ("d :\\ abc \" + (count ++) + ". part "); 22 fos. write (buf, 0, len); 23 fos. close (); 24 25} 26 FI. close (); 27 28} 29}View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.