IO [byte stream, efficient stream], io byte stream
IO stream:
I: input, input, read to memory
O: Output, Output, and file writing
Stream: Data Stream (character, byte)
Category:
Flow Direction:
Input: FileInputStream and FileReader)
Output: FileOutputStream and FileWriter)
Type:
Character, byte
Java. io. OutputStream: The Byte output stream, which is the parent class of all byte output streams.
Common member methods:
Abstract void write (int B) write a byte
Void write (byte [] B) write to byte array
Void write (byte [] B, int off, int len) write to byte array, off is the start index, len writes several
Void close () closes the output stream and releases all system resources related to the stream.
Java. io. FileOutputStream: file byte output stream
Purpose: Write Data in the memory in bytes to the file.
Constructor:
FileOutputStream (File file) creates a File output stream that writes data to the File represented by the specified file object.
FileOutputStream (String name) creates an output file stream that writes data to a file with the specified name.
Parameters of the constructor:
File: file
Name: file path
All are data writing destinations
Features:
If the specified file or file path in the constructor does not point to the file, the constructor creates a file.
If the append write switch is not enabled and the file already exists, it overwrites
Procedure:
1. Create a byte output stream object FileOutputStream to bind the data destination
2. Use the write method in FileOutputStream to write data to a file.
3. release resources
When the stream writes data, it will find the JVM. The JVM will call the local method of the system to complete the writing. After the stream is used, it will use system-related resources (release the memory)
Void write (byte [] B) write to byte array
Void write (byte [] B, int off, int len) write to byte array, off is the start index, len writes several
1 public static void main (String [] args) throws IOException {2 File file = new File ("B .txt"); 3 FileOutputStream fos = new FileOutputStream (file ); 4 // write 100 to the file, and 100 is 3 bytes 5 fos. write (49); 6 fos. write (48); 7 fos. write (48); 8 9/* 10 * void write (byte [] B) when writing a byte array 11 * when writing data, if the number of 12 * bytes written at a time is positive, ASC is queried. | Table 13 * indicates that the first byte is negative, and the second byte can be positive, it can also be a negative number. During the query, the two bytes form a Chinese character and the GBK encoding Table 14 */15 byte [] bytes = {65,66, 67,68, 69} is queried }; // ABCDE16 // byte [] bytes = {-65,-66,-67,68, 88}; // baked into 17 18 fos. write (bytes); 19 20 // void write (byte [] B, int off, int len) write to the byte array, off is the start index, len writes several 21 fos. write (bytes, 1, 2); 22 23/* 24 * methods for fast writing to byte arrays 25 * methods in the String class 26 * byte [] getBytes (String charsetName ): convert string to byte array 27 */28 byte [] bytes2 = "hello ". getBytes (); 29 System. out. println (Arrays. toString (bytes2); // [-60,-29,-70,-61] 30 fos. write (bytes2); 31 32 fos. close (); 33}
Void write (byte [] B) write to byte array
When writing data, if you write multiple bytes at a time
The written bytes are positive: ASC queries | table
The written bytes are: the first byte is a negative number, and the second byte can be a positive number or a negative number. During the query, the two bytes form a Chinese character and query the GBK encoding table.
Fast Writing of byte Arrays
There is a method in the String class.
Byte [] getBytes (String charsetName): converts a String to a byte array.
File Extension and line feed:
Line feed:
Windows: \ r \ n
Linux: \ n
Mac: \ r
Append write: Use the constructor of Two Parameters
FileOutputStream (File file, boolean append)
FileOutputStream (String name, boolean append)
Parameters:
File file, String name: the destination of the written data
Boolean append: Specifies the append write switch. true: append can be performed (to the previous file, continue to write the content). fasle: The append cannot be written (to overwrite the previous file)
Java. io. InputStream: The Byte input stream, which is the parent class of all byte input streams.
Common member methods:
Int read (): reads a byte and returns it. If no byte is returned,-1 is returned.
Int read (byte []): reads a certain number of bytes and stores them in the byte array. The number of bytes read is returned.
Void close () closes the input stream of this file and releases all system resources related to this stream.
Java. io. FileInputStream: file byte input stream
Purpose: Read the data in the file in bytes to the memory.
Constructor:
FileInputStream (String name)
FileInputStream (File file)
Parameter: file to be read (Data Source)
String name: file path of the String
File file: Read File
Procedure:
1. Create a byte input stream object FileInputStream and bind it to the data source.
2. read the file using the method in the FileInputStream object
3. release resources
Int read (byte []): reads a certain number of bytes and stores them in the byte array. The number of bytes read is returned.
Clear:
1. Effect of byte array: It can buffer multiple bytes in the array at a time to improve reading efficiency.
Byte array length: generally defined as 1024 (one kb) or an integer multiple of 1024.
2. What is the return value int: The number of valid bytes read each time
File Replication: reads a file by reading one byte at a time. Writes a file by writing one byte at a time.
Data source: c: \ 1.jpg
Data destination: d: \ 1.jpg
Procedure:
1. Create a byte input stream object FileInputStream and bind it to the data source.
2. Create a byte output stream object FileOutputStream and bind it to the data destination.
3. Use the method read in FileInputStream to read one byte at a time.
4. Use the method write in FileOutputStream to write one byte at a time.
5. Release the resource (first turn off writing and then turn off reading)
1 public static void main (String [] args) throws IOException {2 long s = System. currentTimeMillis (); 3 // 1. create a byte input stream object FileInputStream and bind it to the data source 4 FileInputStream FD = new FileInputStream ("c: \ 1.jpg"); 5 // 2. create a byte output stream object FileOutputStream and bind it to the data destination 6 FileOutputStream fos = new FileOutputStream ("d: \ 1.jpg"); 7 // 3. use the method read in FileInputStream to read a single byte at a time. 8 int len = 0; // receives the read byte 9 while (len = Fi. read ())! =-1) {10 // 4. use the method write in FileOutputStream to write a byte 11 fos at a time. write (len); 12} 13 // 5. release resources (write first and read later) 14 fos. close (); 15. close (); 16 long e = System. currentTimeMillis (); 17 System. out. println (e-s); 18}
File Replication: reads files using byte array buffering. Data is written to multiple bytes at a time.
*
* Data source: c :\\ 1.jpg
* Data destination: d: \ 1.jpg
*
* Procedure:
* 1. Create a byte input stream object FileInputStream and bind it to the data source.
* 2. Create a byte output stream object FileOutputStream and bind it to the data destination.
* 3. read multiple bytes at a time using the method read (byte []) in FileInputStream
* 4. Use the write (byte [], 0, len) method in FileOutputStream to write multiple bytes at a time
* 5. release resources
1 public static void main (String [] args) throws Exception {2 long s = System. currentTimeMillis (); 3 // 1. create a byte input stream object FileInputStream, and bind it to the data source 4 FileInputStream FCM = new FileInputStream ("c: \ z.zip"); 5 // 2. create a byte output stream object FileOutputStream and bind it to the data destination 6 FileOutputStream fos = new FileOutputStream ("d: \ z.zip"); 7 // 3. use the method read (byte []) in FileInputStream to read multiple 8 byte [] bytes = new byte [1024*100] at a time; 9 int len = 0; // read The number of valid bytes is 10 while (len = Fi. read (bytes ))! =-1) {11 // 4. use the method write (byte [], 0, len) in FileOutputStream to write multiple bytes of 12 fos at a time. write (bytes, 0, len); 13} 14 // 5. release resource 15 fos. close (); 16. close (); 17 18 long e = new Date (). getTime (); 19 System. out. println (e-s); 20}
File Replication: Read files using a buffer stream + array, write data using a buffer stream to write multiple
*
* Data source: c :\\ 1.jpg
* Data destination: d: \ 1.jpg
*
* Procedure:
* 1. Create a FileInputStream object and bind it to the data source.
* 2. Create a BufferedInputStream object and input FileInputStream In the constructor to improve the reading efficiency of FileInputStream.
* 3. Create a FileOutputStream object and bind it to the data destination.
* 4. Create a BufferedOutputStream object and pass FileOutputStream In the constructor to improve FileOutputStream efficiency.
* 5. read the object using the read (byte []) method in BufferedInputStream
* 6. Use the method write (byte [], 0, len) in BufferedOutputStream to write data to the buffer zone.
* 7. Use the method flush in BufferedOutputStream to refresh the data in the buffer to the file.
* 8. release resources
1 public static void main (String [] args) throws Exception {2 long s = System. currentTimeMillis (); 3 // 1. create a FileInputStream object and bind it to the data source 4 FileInputStream FCM = new FileInputStream ("c: \ z.zip"); 5 // 2. create a BufferedInputStream object. In the constructor, input FileInputStream 6 BufferedInputStream bis = new BufferedInputStream (FS); 7 // 3. create a FileOutputStream object and bind it to the data destination 8 FileOutputStream fos = new FileOutputStream ("d: \ z.zip"); 9 // 4. Create a BufferedOutputStream object and pass FileOutputStream In the constructor to improve FileOutputStream efficiency. 10 BufferedOutputStream bos = new BufferedOutputStream (fos); 11 // 5. read the file 12/* int len = 0; 13 while (len = bis. read ())! =-1) {14 // 6. use the method write (byte [], 0, len) in BufferedOutputStream to write data to the buffer zone 15 bos. write (len); 16} */17 byte [] bytes = new byte [1024*100]; 18 int len = 0; 19 while (len = bis. read (bytes ))! =-1) {20 fos. write (bytes, 0, len); 21 fos. flush (); 22} 23 24 // 8. release resources 25 bos. close (); 26 bis. close (); 27 28 long e = new Date (). getTime (); 29 System. out. println (e-s); 30}
Java. io. BufferedOutputStream: byte buffer output stream extends OutputStream
* Byte buffer output stream: adds a buffer for the basic stream to improve the efficiency of the Basic stream.
*
* Common member methods inherited from the parent class
* Abstract void write (int B) writes a byte
* Void write (byte [] B) write to byte array
* Void write (byte [] B, int off, int len) is written to the byte array, off is the start index, and len writes several
* Void close () closes the output stream and releases all system resources related to the stream.
*
* Constructor:
* BufferedOutputStream (OutputStream out) creates a new buffer output stream to write data to the specified underlying output stream.
* Parameters:
* OutputStream out: the byte output stream. You can use FileOutputStream.
* When a parameter is passed, the output stream of the byte will be added with a buffer to improve the efficiency of the stream.
* Steps:
* 1. Create a FileOutputStream object and bind it to the data destination.
* 2. Create a BufferedOutputStream object and pass FileOutputStream In the constructor to improve FileOutputStream efficiency.
* 3. Use the write method in BufferedOutputStream to write data to the buffer zone.
* 4. Use the flush method in BufferedOutputStream to refresh the data in the buffer to the file.
* 5. release resources
1 public static void main (String [] args) throws IOException {2 // 1. create a FileOutputStream object and bind it to the data Destination 3 FileOutputStream fos = new FileOutputStream ("buffered.txt"); 4 // 2. create a BufferedOutputStream object and pass FileOutputStream 5 BufferedOutputStream bos = new BufferedOutputStream (fos) in the constructor; 6 // 3. use the method write in BufferedOutputStream to write data to the buffer 7 bos. write (97); 8 9 bos. write ("I Am a buffer stream ". getBytes (); 10 // 4. use the flush method in BufferedOutputStream to refresh the data in the buffer to 11 bos in the file. flush (); 12 // 5. release resource 13 bos. close (); 14}
Java. io. BufferedInputStream: byte buffer input stream extends InputStream
* Function: adds a buffer for the input stream of the basic byte to improve the efficiency of the input stream of the basic byte.
*
* Member methods inherited from the common sending of the parent class:
* Int read (): reads a byte and returns it. If no byte is returned,-1 is returned.
* Int read (byte []): reads a certain number of bytes and stores them in a byte array. The number of bytes read is returned.
* Void close () closes the input stream of this file and releases all system resources related to this stream.
*
* Constructor:
* BufferedInputStream (InputStream in) creates a BufferedInputStream and saves its parameters, that is, the input stream in, for future use.
* Parameters:
* InputStream in: byte input stream, which can be transferred to FileInputStream
* The parameter adds a buffer to the byte input stream object to improve the efficiency of the stream.
*
* Steps:
* 1. Create a FileInputStream object and bind it to the data source.
* 2. Create a BufferedInputStream object and input FileInputStream In the constructor to improve the reading efficiency of FileInputStream.
* 3. read the file by using the method in BufferedInputStream.
* 4. release resources
*
* Conclusion:
* Byte stream: The operated file is a non-text file and the file is copied.
* Ghost stream: all operated files are text files. One character can be read at a time and Chinese characters can be read.
* Text File: Open it in notepad and understand it.
1 public static void main (String [] args) throws IOException {2 // 1. create a FileInputStream object and bind it to the data source 3 FileInputStream FD = new FileInputStream ("buffered.txt"); 4 // 2. create a BufferedInputStream object and input FileInputStream 5 BufferedInputStream bis = new BufferedInputStream (FS) in the constructor; 6 // 3. read the object using the method read in BufferedInputStream. 7 // int read (): read a byte and return it. If no byte is returned,-1 is returned. 8/* int len = 0; 9 while (len = bis. read ())! =-1) {10 System. out. println (char) len); 11} */12 13 // int read (byte []): reads a certain amount of bytes and stores them in a byte array, returns the number of bytes read. 14 byte [] bytes = new byte [1024]; 15 int len = 0; 16 while (len = bis. read (bytes ))! =-1) {17 System. out. println (new String (bytes, 0, len); 18} 19 20 // 4. release resource 21 bis. close (); 22 System. out. println ("-------------------"); 23 BufferedReader br = new BufferedReader (new FileReader ("buffered.txt"); 24 while (len = br. read ())! =-1) {25 System. out. println (char) len); 26} 27}
Copy a single folder
*
* Data source: c :\\ demo
* Data destination: d :\\
*
* Procedure:
* 1. check whether there is a demo folder on disk D. If no demo folder exists, create it.
* 2. Create a copy file
* Return value type: void
* Method name: copyFile
* Parameter list: File src, File dest
* 3. traverse the folder to be copied and obtain the path of each file in the folder (the data source of the file to be copied)
* 4. Use the method getName in the file class to splice the data of the file to be copied
* 5. Call the copyFile replication method for copying
1 public static void main (String [] args) throws IOException {2 // 1. check whether there is a demo folder on disk d. if not, create 3 File file = new File ("d: \ demo"); 4 if (! File. exists () {5 file. mkdirs (); 6} 7 // 3. traverse the folder to be copied and obtain the path of each File in the folder (the data source of the File to be copied) 8 File srcDirectory = new File ("c: \ demo "); 9 File [] srcFiles = srcDirectory. listFiles (); 10 for (File srcFile: srcFiles) {11 // 4. use the method getName in the file class to splice the data of the file to be copied. Objective 12 String srcName = srcFile. getName (); 13 // use the third constructor of the File class to create the destination 14 File destFile = new File (file, srcName); 15. call the copyFile replication method to copy 16 copyFile (srcFile, destFile); 17} 18}