Java Foundation - Character Buffered Stream of IO Stream Objects (BufferedWriter and BufferedReader)

            Java Foundation - Character Buffered Stream of IO Stream Objects (BufferedWriter and BufferedReader)

                                    Author: Yin Zhengjie

Copyright statement: original works, please do not reprint! Otherwise held liable.

 

 

 

 

1. Character buffer stream

  There are 2 character buffer streams according to the direction of the stream:

      1>. Write data to the stream, byte buffer output stream BufferedReader

      2>. Read the data in the stream, byte buffer input stream BufferedWriter

  Complete efficient writing and reading of text data.

 

2. Character output buffer stream ( BufferedWriter )

 BufferedWriter has a unique method called newLine, which can solve the problem of different line breaks between Windows and Linux.

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedWriter;
10 import java.io.FileWriter;
11 import java.io.IOException;
12 
13 public class BufferedWriteDemo {
14     public static void main(String[] args) throws IOException {
15         // Create a character output stream and encapsulate the file 
16          FileWriter fw = new FileWriter("yinzhengjie.txt" );
 17          BufferedWriter bfw = new BufferedWriter(fw);
 18          // Write a character 
19          bfw.write(97 );
 20          / / Write a newline, whether your operating system is Linux or Windows, it will automatically handle the newline for you 
21          bfw.newLine();
 22          bfw.flush();
 23          // Write a character array 
24          bfw.write( "Yin Zhengjie" .toCharArray());
 25          // Write a newline, whether your operating system is Linux or Windows, it will automatically handle the newline for you 
26          bfw.newLine();
 27         bfw.flush();
 28          // Write a string 
29          bfw.write("yinzhengjie" );
 30          // Write a newline, whether your operating system is Linux or Windows, it will automatically handle the newline for you 
31          bfw .newLine();
 32          bfw.flush();
 33          bfw.close();
 34          
35      }
 36 }

 

 3. Character buffered input stream (BufferedReader)

 

  Reads text from a character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and lines.

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedReader;
10 import java.io.FileReader;
11 import java.io.IOException;
12 
13 public class BufferedReaderDemo {
14     public static void main(String[] args) throws IOException {
15         // Create a character input stream buffer stream object, the constructor passes the character input stream, and wraps the data source file 
16          BufferedReader bfr = new BufferedReader( new FileReader("yinzhengjie.txt" ));
 17          // Call the buffered stream method readLine() Read text line
 18          // The text line is read in a loop, and the end condition readLine() returns null. 
19          String line;
 20          // Define the counter to define the line number. Of course, BufferedReader also has a subclass LineNumberReader that can also read the line number. 
21          int lineNumber = 0 ;
 22          while ( ( line = bfr.readLine()) != null ) {
 23              lineNumber++ ;
 24              //At this time, the line itself has no newline character, because the newline character has been taken off by readLine, so we should call println. 
25              System.out.println(lineNumber+" "+ line);
 26          }
 27          bfr.close();
 28      }
 29 }

 

Four. Character buffer stream copy text file

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedReader;
10 import java.io.BufferedWriter;
11 import java.io.FileReader;
12 import java.io.FileWriter;
13 import java.io.IOException;
14 
15 public class CopyFile {
16     public static  void main(String[] args) throws IOException {
 17          BufferedReader bfr = new BufferedReader( new FileReader("yinzhengjie.txt" ));
 18          BufferedWriter bfw = new BufferedWriter( new FileWriter("yinzhengjie.backup" ));
 19          
20          // Read text line, read one line, write one line, write newline 
21          String line;     // default is null 
22          while ( (line = bfr.readLine()) != null ) {
 23              bfw.write(line);
 24             bfw.newLine();
25             bfw.flush();
26         }
27         bfw.close();
28         bfr.close();
29     }
30 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214772&siteId=291194637