Java Buffered Stream Buffer

Source: Internet
Author: User

Buffered Stream buffer: Set buffer for faster execution efficiency

Sub-class:

(i) Bufferedinputstream: buffer input byte stream, purpose: Improve the efficiency of reading files
Note: Bufferedinputstream He is not capable of reading and writing data
Internal implementation: You maintain a byte array of 8 bytes.
Steps to use:
1. Locate a target file.
2. Establish the channel new FileInputStream ("");
3. Create a buffer byte input stream new Bufferedinputstream (FileInputStream);
4. Reading data read ();
5. Closing resources close ();


(ii) Bufferedoutputstream: buffered output byte stream internally maintains a 8k array of bytes
Effect: Improve the efficiency of the output of the file, can provide other methods.
Use:
1. Find a target
2. Establish the channel FileOutputStream
3. Create a buffered byte output stream
4. Write data that is not written to disk. If the data in the array is already full, the data is automatically written to disk.
5. Write data to disk: Call Flush (), or close the resource.
6. Close the resource.


(iii) Buffredread buffer input character stream.
There is an extended function: ReadLine (); You can read one line of text at a time.

(iv) BufferedWriter: buffered output character stream
Internally provides a 8192-length character array as such a buffer.
BufferedWriter effect: Improve the efficiency of writing, expand the function of FileWriter.

NewLine (); Line break Write Data

Simple byte-buffered stream case

1 ImportJava.io.BufferedInputStream;2 ImportJava.io.BufferedOutputStream;3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8  Public classBuffered {9 Ten     /** One      * @paramargs A      * @throwsIOException -      */ -      Public Static voidMain (string[] args)throwsIOException { the         //TODO auto-generated Method Stub -         //bufintest (); - bufouttest (); -     } +      -     //(1) Use of Bufferedinputstream +      Public Static voidBufintest ()throwsioexception{ A         //1. Find the target atFile File =NewFile ("D:\\a.txt"); -         //2. Create a channel -FileInputStream FIS =Newfileinputstream (file); -  -         //**3. Creating a buffered input byte stream ****** -Bufferedinputstream Bfis =NewBufferedinputstream (FIS); in  -         //4. Start writing data to          intContent = 0;//only one byte is taken at a time +          while((content= bfis.read ())! =-1) {//or is one of the read ask what can improve efficiency ? -System.out.print ((Char) content); the         } *         //5. Close the channel $ bfis.close ();Panax Notoginseng     } -      the     //(2) Use of Bufferedoutputstream +      Public Static voidBufouttest ()throwsioexception{ A          the         //1. Find a target +File File =NewFile ("D:\\b.txt"); -         //2. Create a channel $FileOutputStream fos =Newfileoutputstream (file); $          -         //3. Create a buffered stream -Bufferedoutputstream Bfos =NewBufferedoutputstream (FOS); the          -         //4. Create data to write to the fileWuyiString string = "Good Good study Day"; the          -         //5. Write the data to this step simply to save the data in an in-memory byte array.  Wu Bfos.write (String.getbytes ()); -          About         //6. Refresh to write data to disk $         //Bfos.flush (); -          -         //7. Close Resources -Bfos.close ();//Flush () is implemented internally; A     } +}

Simple character Buffer stream case

1 ImportJava.io.BufferedReader;2 ImportJava.io.BufferedWriter;3 ImportJava.io.File;4 ImportJava.io.FileReader;5 ImportJava.io.FileWriter;6 Importjava.io.IOException;7 8  Public classFileReader {9 Ten      Public Static voidMain (string[] args)throwsIOException { One Testbufferedwriter (); A Testbufferedread (); -     } -         //(1) Use of buffered output character streams the      Public Static voidTestbufferedwriter ()throwsioexception{ -          -         //1. Set up a channel to specify a path -FileWriter FIW =NewFileWriter ("D:\\a.txt",true); +  -         //2. Create a buffered stream +BufferedWriter BFW =NewBufferedWriter (FIW); A  at         //let the data wrap the display -Bfw.newline ();//line break Write Data -  -         //3. Start writing data -Bfw.write ("Don't eat in class.")); -  in         //4. Close Resources -Bfw.close ();//A refresh operation is done before the resource is closed.    Call the Flush () method.  to     } +      -     //(2) Use of buffered input character streams the      Public Static voidTestbufferedread ()throwsioexception{ *          $         //1. Create a channel and specify a pathPanax NotoginsengFileReader fir =NewFileReader ("D:\\b.txt"); -  the         //2. Create a buffered stream +BufferedReader BFR =NewBufferedReader (FIR); A  the         //3.1. Start reading data (read one byte at a time) +         intContent = 0; -          while(content = Bfr.read ())! =-1) { $              $System.out.print ((Char) content); -         } -          the         //3.2, ReadLine () extended function, read a row of data -String string1 =bfr.readline ();Wuyi System.out.println (string1); the          -         //Higher efficiency by reading one row of data at a time (return string) WuString string =NULL; -          while(String = Bfr.readline ())! =NULL) { About System.out.println (string); $         } -          -         //4. Close - bfr.close (); A     } +         

Java Buffered Stream Buffer

Related Article

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.