One, buffer flow:
Java provides a buffer stream, which can improve the read and write speed of Io stream .
Buffer stream, according to the classification of the stream is divided into: byte buffer stream and character Buffer stream .
Two, byte buffer stream:
The byte buffer stream is based on the direction of the flow , with 2 total:
1. write data to stream, byte buffered output stream bufferedoutputstream
2. read the data in the stream, byte buffer input stream Bufferedinputstream
Their internal contains a buffer , read and write through the buffer , can improve the IO stream read and write speed
2.1 Byte buffered output stream Bufferedoutputstream class
Public classBufferedOutputStreamDemo01 { Public Static voidMain (string[] args)throwsIOException {//ways to write data to a filewrite (); } /** Write data to File method * 1, create stream * 2, write data * 3, close stream*/ Private Static voidWrite ()throwsIOException {//Create a basic byte output streamFileOutputStream fileout =NewFileOutputStream ("Abc.txt"); //use efficient streams to encapsulate basic streams and achieve faster speedsBufferedoutputstream out =NewBufferedoutputstream (fileout); //2, write DataOut.write ("Hello". GetBytes ()); //3, close the streamOut.close (); }}
2.2-byte buffered input stream Bufferedinputstream class
Private Static voidRead ()throwsIOException {//1. Create a buffered Stream objectFileInputStream Filein =NewFileInputStream ("Abc.txt"); //wrap the basic flow into an efficient flowBufferedinputstream in =NewBufferedinputstream (Filein); //2, read the data intch =-1; while(ch = in.read ())! =-1 ) { //PrintSystem.out.print ((Char) ch); } //3, CloseIn.close (); }
Practice Questions: copying files using buffered byte streams
Packagecom.oracle.demo02;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;//Buffer byte stream Public classbuffercopy { Public Static voidMain (string[] args)throwsIOException {Longtime1=System.currenttimemillis (); //1. Determine the data sourceFileInputStream fis=NewFileInputStream ("E:\\java\\eclipse.zip"); Bufferedinputstream bis=NewBufferedinputstream (FIS); //2. Identify the target placeFileOutputStream fos=NewFileOutputStream ("D:\\java\\java\\dandan\\eclipse.zip"); Bufferedoutputstream Bos=NewBufferedoutputstream (FOS); //3. Read and write intLen=0; byte[] bytes=New byte[1024]; while((Len=bis.read (bytes))!=-1) {bos.write (bytes); } //4. Refresh and Release ResourcesBos.flush (); Bos.close (); Bis.close (); LongTime2=System.currenttimemillis (); System.out.println ("Time Spent:" + (time2-time1)/1000+ "SEC"); }}
Three, character Buffer stream
1. Character buffered input stream BufferedReader
2. Character buffered output stream BufferedWriter
Efficient write-and-read operations that complete text data
3.1-character buffered input stream BufferedReader class
Reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows.
Special methods:
readLine () reads a line of text that contains a string containing the contents of the line, does not contain any line terminators, and returns null if the end of the stream has been reached
Packagecom.oracle.Demo01;ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException; Public classDemo02 {//character buffer input stream Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method StubFileReader fr=NewFileReader ("E:\\java\\output.txt"); BufferedReader BR=NewBufferedReader (FR); //a method that reads a line of text. ReadLine () return value is string with NULL to control the loop//While Loop method readsString str=NULL; while((Str=br.readline ())! =NULL){//When the return value is NULL, the description does not read the characterSystem.out.println (str); } //Normal Method Read//String str1=br.readline ();//System.out.println (str1);//String str2=br.readline ();//System.out.println (str2);//String str3=br.readline ();//System.out.println (STR3); //NULL is returned when the content is not readBr.close (); }}
3.2-character buffered output stream BufferedWriter class
Writes text to the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings.
Special methods:
NewLine () writes a line break based on the current system.
Packagecom.oracle.Demo01;ImportJava.io.BufferedWriter;ImportJava.io.FileWriter;Importjava.io.IOException; Public classDemo01 {//character buffered output stream Public Static voidMain (string[] args)throwsIOException {//1. Determine the destinationFileWriter fw=NewFileWriter ("E:\\java\\output.txt",true); //2. Package as a character buffer streamBufferedWriter bw=NewBufferedWriter (FW); //3. Writing DataBw.write ("How are You?")); Bw.newline (); //character buffer output stream, line wrapping method:. NewLine ();Bw.flush (); Bw.write ("I'm fine."); //Refresh stream does not close the stream, so you can continue to output bw.close (); }}
Practice title: Using buffered streams for file copying
Packagecom.oracle.Demo01;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException; Public classbufferedcopy {//replication of buffered character streams Public Static voidMain (string[] args)throwsIOException {//1. Get the data sourceFileReader fr=NewFileReader ("E:\\java\\output.txt"); //2. Encapsulating into buffered streamsBufferedReader br=NewBufferedReader (FR); //3. Get the destinationFileWriter fw=NewFileWriter ("E:\\java\\copy.txt",true); //4. Encapsulating into buffered streamsBufferedWriter bw=NewBufferedWriter (FW); //5. Read and writeString str=NULL; while((Str=br.readline ())! =NULL) {bw.write (str); Bw.newline (); //Because the read is read by the text line method, so in order to format uniform, you need to add a newline method } //6. Releasing ResourcesBw.newline (); Bw.close (); Br.close (); }}
Java Phase III Learning (quad, buffered stream)