Java Learning Note 40 (buffered stream)

Source: Internet
Author: User

Buffered streams:

In the various streams of reading and writing files, the most disturbing is the efficiency problem,

The goal of buffer flow is to improve reading and writing efficiency.

BYTE output buffer stream:

 Packagedemo;ImportJava.io.BufferedOutputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;//Improve Write efficiency Public classBufferedoutputstreamdemo { Public Static voidMain (string[] args)throwsIOException {fileoutputstream fos=NewFileOutputStream ("D:\\buffer.txt"); Bufferedoutputstream Bos=NewBufferedoutputstream (FOS); Bos.write (66); byte[] bytes = "HelloWorld". GetBytes ();        Bos.write (bytes);    Bos.close (); }}

byte input buffer stream:

 Packagedemo;ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;Importjava.io.IOException; Public classBufferedinputstreamdemo { Public Static voidMain (string[] args)throwsIOException {Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream ("D:\\buffer.txt")); byte[] bytes =New byte[10]; intLen = 0;  while(len = bis.read (bytes))! =-1) {System.out.print (NewString (Bytes, 0, Len));    } bis.close (); }}

You can use buffered streams to copy files and compare them to previous methods:

and compare the copy time

 Packagedemo;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classCopy { Public Static voidMain (string[] args)throwsIOException {Longs =System.currenttimemillis (); Copy1 (NewFile ("D:\\lol.exe"),NewFile ("E:\\lol.exe")); LongE =System.currenttimemillis (); System.out.println (e-s);//14154 milliseconds (14 seconds) copiedCopy2 (NewFile ("D:\\lol.exe"),NewFile ("E:\\lol.exe")); //Same method Test time: 129 milliseconds (0.1 seconds)Copy2 (NewFile ("D:\\lol.exe"),NewFile ("E:\\lol.exe")); //Test Time: 94 milliseconds (less than 0.1 seconds)    }     Public Static voidCopy1 (file src, file desc)throwsIOException {fileinputstream fis=NewFileInputStream (SRC); FileOutputStream Fos=NewFileOutputStream (DESC); intLen = 0;  while(len = Fis.read ())! =-1) {fos.write (len);        } fos.close ();    Fis.close (); }     Public Static voidCopy2 (file src, file desc)throwsIOException {fileinputstream fis=NewFileInputStream (SRC); FileOutputStream Fos=NewFileOutputStream (DESC); intLen = 0; byte[] bytes =New byte[1024];  while(len = fis.read (bytes))! =-1) {fos.write (bytes,0, Len);        } fos.close ();    Fis.close (); }     Public Static voidCopy3 (file src, file desc)throwsIOException {Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream (SRC)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (DESC)); intLen = 0; byte[] bytes =New byte[1024 * 10];  while(len = bis.read (bytes))! =-1) {bos.write (bytes,0, Len);        } bos.close ();    Bis.close (); }}

Character buffered output stream:

 Packagedemo;ImportJava.io.BufferedWriter;ImportJava.io.FileWriter;Importjava.io.IOException; Public classbufferedwriterdemo{ Public Static voidMain (string[] args)throwsIOException {write (); }     Public Static voidWrite ()throwsioexception{FileWriter FW=NewFileWriter ("D:\\buffer.txt"); BufferedWriter BFW1=NewBufferedWriter (FW); Bfw1.write (100);        Bfw1.flush (); Bfw1.write (Hello. ToCharArray ()); Bfw1.newline ();//Special Line Wrapping method//you can use \ r \ n to wrap, but we recommend this approach, which is platform agnosticBfw1.flush (); Bfw1.write ("HelloWorld");        Bfw1.flush ();            Bfw1.close (); }}

Character buffer input stream:

 Packagedemo;ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException; Public classBufferedreaderdemo { Public Static voidMain (string[] args)throwsIOException {read (); }     Public Static voidRead ()throwsIOException {intlinenumber = 0; BufferedReader BFR1=NewBufferedReader (NewFileReader ("D:\\read.txt")); //buffered stream-specific methods, reading a single line of textString line =NULL;  while(line = Bfr1.readline ())! =NULL) {linenumber++; System.out.println ("The contents of" + LineNumber + "line:" +Line );    } bfr1.close (); }}

Character buffer stream copy text file:

 Packagedemo;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException; Public classCopy { Public Static voidMain (string[] args)throwsIOException {BufferedReader BFR1=NewBufferedReader (NewFileReader ("D:\\read.txt")); BufferedWriter BFW1=NewBufferedWriter (NewFileWriter ("E:\\read.txt")); String Line=NULL;  while(line = Bfr1.readline ())! =NULL) {bfw1.write (line);            Bfw1.newline ();        Bfw1.flush ();        } bfw1.close ();    Bfr1.close (); }}

The operation rules and selection of various flow:

1. Clear whether to read or write (source and destination)

2. What type of, byte, or text do you want to manipulate?

3. What is the device that the data resides on, on the hard disk or in memory, or on the network? (Memory stream and socket are not described here)

4. Do I need to encode the conversion, need to use buffer stream, array to improve efficiency code?

Java Learning Note 40 (buffered stream)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.