Java Buffer buffer and java Buffer
Buffer stream Buffer: sets the Buffer to accelerate execution efficiency
Subclass:
(1) BufferedInputStream: buffer the input byte stream. Objective: To improve file reading efficiency
Note: BufferedInputStream has no data read/write function.
Internal implementation: you maintain an 8-byte array.
Procedure:
1. Find a target file.
2. Create a channel new FileInputStream ("");
3. Create a buffer byte input stream new BufferedInputStream (FileInputStream );
4. read data read ();
5. close resource close ();
(2) BufferedOutputStream: an 8 K byte array is maintained inside the buffer output byte stream.
Purpose: Improve the efficiency of file output and provide other methods.
Usage:
1. Find the target
2. Create a channel FileOutputStream
3. Create a buffer byte output stream
4. Write data, which is not written to the disk. If the data in the array is full, the data is automatically written to the disk.
5. Write Data to the disk: Call flush () or close the resource.
6. Close the resource.
(3) BuffredRead buffered input streams.
There is an extended function: readLine (); // you can read a line of text at a time.
(4) BufferedWriter: buffer the Output Response stream
An array of 8192 characters is provided internally as a buffer.
BufferedWriter: Improves the write efficiency and expands the FileWriter function.
NewLine (); // line feed write data
Simple byte buffer Stream case
1 import java. io. bufferedInputStream; 2 import java. io. bufferedOutputStream; 3 import java. io. file; 4 import java. io. fileInputStream; 5 import java. io. fileOutputStream; 6 import java. io. IOException; 7 8 public class buffered {9 10/** 11 * @ param args12 * @ throws IOException 13 */14 public static void main (String [] args) throws IOException {15 // TODO Auto-generated method stub16 // bufInTest (); 17 B UfOutTest (); 18} 19 20 // (1) use of BufferedInputStream 21 public static void bufInTest () throws IOException {22 // 1. find the target 23 File file = new File ("D: \ a.txt"); 24 // 2. create Channel 25 FileInputStream FCM = new FileInputStream (file); 26 27 // *** 3. create a buffer input byte stream ***** 28 BufferedInputStream bfis = new BufferedInputStream (FCM); 29 30 // 4. 31 int content = 0; // only one byte 32 while (content = bfis. read ())! =-1) {// or is it possible to improve the efficiency of reading questions one by one? 33 System. out. print (char) content); 34} 35 // 5. disable channel 36 bfis. close (); 37} 38 39 // (2) Use of BufferedOutputStream 40 public static void bufOutTest () throws IOException {41 42 // 1. find the target 43 File file = new File ("D: \ B .txt"); 44 // 2. create channel 45 FileOutputStream fos = new FileOutputStream (file); 46 47 // 3. create a buffer stream 48 BufferedOutputStream bfos = new BufferedOutputStream (fos); 49 50 // 4. create data to write to file 51 String string = "good stu Dy day up "; 52 53 // 5. Write data. In this step, only the data is saved to the byte array in the memory. 54 bfos. write (string. getBytes (); 55 56 // 6. refresh and write the data to the disk. flush (); 58 59 // 7. disable resource 60 bfos. close (); // internal flush (); 61} 62}
Simple case of Character Buffer stream
1 import java. io. bufferedReader; 2 import java. io. bufferedWriter; 3 import java. io. file; 4 import java. io. fileReader; 5 import java. io. fileWriter; 6 import java. io. IOException; 7 8 public class fileReader {9 10 public static void main (String [] args) throws IOException {11 testBufferedWriter (); 12 testBufferedRead (); 13} 14 // (1) Use 15 public static void testBufferedWriter () throws IOException for buffered output streams {16 17 // 1. create a channel and specify a path 18 FileWriter fiw = new FileWriter ("D: \ a.txt", true); 19 20 // 2. create a buffer stream 21 BufferedWriter bfw = new BufferedWriter (fiw); 22 23 // Let the data wrap display 24 bfw. newLine (); // line feed write data 25 26 // 3. start writing data 27 bfw. write ("do not eat in class"); 28 29 // 4. close resource 30 bfw. close (); // a refresh operation is performed before the resource is closed. Call the flush () method. 31} 32 33 // (2) use 34 public static void testBufferedRead () throws IOException {35 36 // 1. create a channel and specify the path 37 FileReader fir = new FileReader ("D: \ B .txt"); 38 39 // 2. create a buffer stream 40 BufferedReader bfr = new BufferedReader (fir); 41 42 // 3.1. Start to read data (read one byte at a time) 43 int content = 0; 44 while (content = bfr. read ())! =-1) {45 46 System. out. print (char) content); 47} 48 49 // 3.2, readLine () extended function, read a row of data 50 String string1 = bfr. readLine (); 51 System. out. println (string1); 52 53 // read a row of data at a time (return String), with a higher efficiency 54 string = null; 55 while (String = bfr. readLine ())! = Null) {56 System. out. println (string); 57} 58 59 // 4. close 60 bfr. close (); 61} 62}