BufferedReader
BufferedReader is a buffered character input stream. It inherits from Reader.
The purpose of BufferedReader is to add some buffering function to other character input streams.
When creating a BufferReader, we will specify a Reader as a parameter through its constructor. The BufferReader will read the data in the Reader in batches, and each time a part of it is read into the buffer; after operating this part of the data in the buffer, the next part of the data is read from the Reader.
Why do I need buffering? The reason is very simple, efficiency issues! The data in the buffer is actually stored in memory, while the raw data may be stored in hard disk or NandFlash; we know that reading data from memory is at least 10 times faster than reading data from hard disk.
Then why not just read all the data into the buffer at one time? First, it may take a long time to read all the data. Second, the memory price is very expensive, and the capacity is not as large as the hard drive.
Example:
import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.File;import java.io.InputStream;import java.io.FileReader;import java.io.IOException;import java.io.FileNotFoundException;import java.lang.SecurityException;public class BufferedReaderTest { private static final int LEN = 5; public static void main(String[] args) { testBufferedReader() ; } /** * BufferedReader API test function */ private static void testBufferedReader() { // Create a BufferedReader character stream, which is the ArrayLetters array try { File file = new File("bufferedreader.txt"); BufferedReader in = new BufferedReader( new FileReader(file)); // Read 5 characters from the character stream. "abcde" for (int i=0; i<LEN; i++) { // If you can continue to read the next character, read the next character if (in.ready()) { // Read "Next character of the character stream" int tmp = in.read(); System.out.printf("%d : %c/n", i, tmp); } } // If "this character stream" does not support the marking function, exit directly if (!in.markSupported()) { System.out.println("make not supported!"); return ; } // Mark "current index position", that is, mark the element of the 6th position -- "f" // 1024 corresponds to marklimit in.mark(1024); // Skip 22 characters. in.skip(22); // Read 5 characters char[] buf = new char[LEN]; in.read(buf, 0, LEN); System.out.printf("buf=%s/n", String.valueOf(buf)); // Read the remaining data of the line System.out.printf("readLine=%s/n", in.readLine()); // Reset the "index of the input stream" to the position marked by mark(), that is, reset to "f". in.reset(); // Read 5 characters from "Reset Character Stream" into buf. That is, read "fghij" in.read(buf, 0, LEN); System.out.printf("buf=%s/n", String.valueOf(buf)); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} The content of bufferedreader.txt read in the program is as follows:
abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
Running results:
0 : a1 : b2 : c3 : d4 : ebuf=01234readLine=56789buf=fghij
BufferedWriter
BufferedWriter is a buffered character output stream. It inherits from Writer.
The purpose of BufferedWriter is to add some buffering functionality to other character output streams.
BufferedWriter buffers data through a character array. When the buffer is full or the user calls the flush() function, it writes the buffer's data to the output stream.
Example:
import java.io.BufferedWriter;import java.io.File;import java.io.OutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.FileNotFoundException;import java.lang.SecurityException;import java.util.Scanner;public class BufferedWriterTest { private static final int LEN = 5; // Corresponding to the English letter "abcdefghijklmnopqrstuvwxyz" //private static final char[] ArrayLetters = "abcdefghijklmnopqrstuvwxyz"; private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; public static void main(String[] args) { testBufferedWriter() ; } /** * BufferedWriter API test function*/ private static void testBufferedWriter() { // Create the BufferedWriter corresponding to the "File Output Stream" // Its corresponding buffer size is 16, that is, when the buffer data >=16, the content of the buffer will be automatically written to the output stream. try { File file = new File("bufferwriter.txt"); BufferedWriter out = new BufferedWriter( new FileWriter(file)); // Write the first 10 characters of the ArrayLetters array to the output stream out.write(ArrayLetters, 0, 10); // Write "line break/n" to the output stream out.write('/n'); out.flush(); // readUserInput() ; out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Read user input*/ private static void readUserInput() { System.out.println("please input a text:"); Scanner reader=new Scanner(System.in); // Wait for an input String str = reader.next(); System.out.printf("the input is : %s/n", str); }}Running result: The file "bufferwriter.txt" is generated, and the content of the file is "abcdefghij".