1. Buffer flow principle
-
Add a buffer (array) to the transmission of basic stream, so that bytes / characters are no longer transmitted one by one, but "packed" in the array, one-time transmission, high efficiency
2. Byte buffer stream
2.1 byte buffered output stream
- Use steps:
- Create a FileOutputStream class object and construct a method to fill in the output destination
- Create the BufferedOutputStream class object, and the constructor fills in the FileOutputStream class object just created
- Call write() method to write data to buffer
- Call the flush() method to refresh the buffer data to the hard disk
- Call close() method to release memory
import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo03 { public static void main(String[] args) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream("G:\\Java\\Test folder\\d.txt"); BufferedOutputStream buffer = new BufferedOutputStream(fileOutputStream); byte[] bytes = {97,98,99,100}; buffer.write(bytes); buffer.flush(); buffer.close(); } }
2.2 byte buffered input stream
- Use steps:
- Create a FileInputStream class object and construct a method to fill in the data source
- Create a BufferedInputStream class object, and the constructor fills in the FileInputStream class object just created
- Call read() method, write data to buffer
- Call close() method to release memory
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class Demo04 { public static void main(String[] args) throws IOException { FileInputStream file = new FileInputStream("G:\\Java\\Test folder\\d.txt"); BufferedInputStream buffer = new BufferedInputStream(file); byte[] bytes =new byte[1024]; int len = 0; while ((len=buffer.read(bytes))!=-1){ System.out.println(new String(bytes,0,len)); } buffer.close(); } }
3. Character buffer stream
3.1 character buffered output stream BufferedWriter
-
Unique method: wrap object. newLine(), no need to consider the system
-
Use steps:
- Create a Writer class object and construct a method to fill in the output destination
- Create the BufferedWriter class object, and the constructor fills in the Writer class object just created
- Call write() method to write data to buffer
- Call the flush() method to refresh the buffer data to the hard disk
- Call close() method to release memory
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class Demo06 { public static void main(String[] args) throws IOException { FileWriter file = new FileWriter("G:\\Java\\Test folder\\e.txt"); BufferedWriter buffer = new BufferedWriter(file); char[] chars = {'a','b','c'}; buffer.write(chars); buffer.newLine();//Wrap, no need to consider system buffer.flush(); char[] chars1 = {'you','good'}; buffer.write(chars1); buffer.flush(); buffer.close(); } }
3.2 character buffered input stream BufferedReader
-
Unique method: object. readLine() reads one line of text (one line is subject to the newline symbol)
-
Use steps:
- Create Reader class object and construct method to fill in input source
- Create the BufferedReader class object, and fill in the Reader class object just created by the construction method
- Call the reader()/readLine() method to read all / a row of data
- Call close() method to release memory
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; public class Demo07 { public static void main(String[] args) throws IOException { FileReader file = new FileReader("G:\\Java\\Test folder\\f.txt"); BufferedReader buffer = new BufferedReader(file); String str = buffer.readLine(); System.out.println(str); buffer.close(); } }
Published 103 original articles, won praise 1, visited 2520