In Java, the use of BufferedReader and BufferedWriter

Buffer stream: (improve the efficiency of reading and writing)

  • BufferedReader, BufferedWriter: used to process characters
  • The buffer stream acts on the node stream, so the object of the node stream is passed to the buffer stream as a parameter

Methods in BufferedReader:

  • read(): read word by word
  • read(char[] ch) reads the length of an array at one time with the of the array as the container
  • readLine() reads one line at a time, reads one line at a time, and reads one line at a time
    Note: the above three methods are read iteratively and will affect each other, so it is not recommended to use them together, which is prone to errors

Methods in BufferedWriter:

  • write(String str): writes a string
  • write(char[] ch): write char array
  • write(char[] ch,int index,int len): the stored data is [0, len] from the ch array

Use input stream:
① Create a corresponding flow variable outside try catch finally and assign null, because the operation of closing the flow should be carried out in finally. If this variable is written in try catch, the scope of this variable will be exceeded in finally
② Instantiate the object of File class
③ The instantiation of the flow (received through the variable in 1). The buffer flow acts on the node flow, so the object of the node flow is passed to the buffer flow as a parameter
④ Read in of data pair
⑤ Close the flow through close() Note: to judge whether the instantiation in 2 is successful, an exception will be thrown at this time, so a try catch is also used to handle the exception
Close the flow of the outer layer (buffer flow) first, and then close the flow of the inner layer (node flow)

Use output stream:
① Create a corresponding flow variable outside try catch finally and assign null, because the operation of closing the flow should be carried out in finally. If this variable is written in try catch, the scope of this variable will be exceeded in finally
② Instantiate the object of File class
③ Instantiation of flow. new class name (through the variable in 1, false), true means append; The default value is false, indicating overwrite. The buffer stream acts on the node stream, so the object of the node stream is passed to the buffer stream as a parameter
④ Writing out data pairs can transfer string, array and basic data types
⑤ Close the flow through close(). Note: to judge whether the instantiation in 2 is successful, an exception will be thrown at this time, so a try catch is also used to handle the exception
Close the flow of the outer layer (buffer flow) first, and then close the flow of the inner layer (node flow)

Byte array char []: ch

Code (read and store text files):

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedReader_BufferedWriterTest {
	public static void main(String[] args) {
		BufferedReader_BufferedWriterTest brw = new BufferedReader_BufferedWriterTest();
		brw.test();
		System.out.println("ok");
	}
	
	public void test() {
		BufferedReader br = null;
		BufferedWriter bw = null;
		
		try {
			File file = new File("src\\I_O flow\\hello.txt");
			File file2 = new File("src\\I_O flow\\hello4.txt");
			
			//The buffer stream acts on the node stream, so the object of the node stream is passed to the buffer stream as a parameter
			br = new BufferedReader(new FileReader(file));
			bw = new BufferedWriter(new FileWriter(file2));
			
			char[] ch = new char[5];
			int len;
			while((len=br.read(ch))!=-1) { //Read the data and save it to the ch array
				bw.write(ch, 0, len);  //Write data. The stored data is [0, len] from the ch array
			}
			
			//readLine() reads one line at a time, reads one line at a time, and reads one line at a time
//			String OneLineData;
//			while((OneLineData=br.readLine())!=null) {
//				bw.write(OneLineData);
//				OneLineData=br.readLine();
//			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (bw != null)
					bw.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
}

Tags: Java

Posted by whitsey on Thu, 19 May 2022 12:03:45 +0300