"Buffer stream of Io stream for Java"

Source: Internet
Author: User

Java The buffer stream itself has no IO function, but adds buffering to the other streams to improve efficiency, such as loading a wrapper over another stream . When the file or other target frequently read or write or operate inefficient, poor performance. This allows for more efficient reading and writing of information using buffered streams. Because the buffered stream caches the data first and then writes or reads it together. So, the buffer stream is still very important, in the IO operation remember to add buffer stream to improve performance.


Buffer stream divided into byte and character buffer streams

The byte buffer stream is:

bufferedinputstream-byte input buffer stream

bufferedoutputstream-byte output buffered stream

The character Buffer stream is:

bufferedreader-character input buffer stream

bufferedwriter-character output Buffer stream

The following mainly describes the use of these four kinds of buffer streams.


A. Byte buffer stream

1.bufferedoutputstream-byte output buffered stream

The Bufferedoutputstream class implements buffered output, and by setting this output stream, the application can write individual bytes to the underlying output stream without having to call the underlying system for every byte write.

The constructor for this class:

650) this.width=650; "id=" aimg_299 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132740e0hp5nif7h47x70h.png "class=" Zoom "width=" 577 "alt=" 132740e0hp5nif7h47x70h.png "/>

Example code:

  1. public static void Main (string[] args) {

  2. try {

  3. Creating a byte output stream instance

  4. OutputStream out=new FileOutputStream ("L:\\test.txt");

  5. Build byte buffer stream based on byte output stream

  6. Bufferedoutputstream buf=new Bufferedoutputstream (out);


  7. String data= "Good study, day up";

  8. Buf.write (Data.getbytes ());//write Buffer

  9. Buf.flush ();//flush buffer, i.e. write content to

  10. Close the stream

  11. Buf.close ();//The buffer is also refreshed once the buffer stream is closed

  12. Out.close ();


  13. } catch (IOException e) {

  14. E.printstacktrace ();

  15. }


  16. }

Copy Code

2.bufferedinputstream-byte input buffer stream

Bufferedinputstream adds buffering capabilities to other input streams, creating an internal buffer array to buffer the data and improve performance when creating Bufferedinputstream.

650) this.width=650; "id=" aimg_300 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132807szrd2hb3qzqdv3wd.png "class=" Zoom "width=" 504 "alt=" 132807szrd2hb3qzqdv3wd.png "/>

Example code:

  1. public static void Main (string[] args) {

  2. try {

  3. Create a byte input stream instance

  4. InputStream in=new FileInputStream ("L:\\test.txt");

  5. Building byte buffer streams based on byte input stream

  6. Bufferedinputstream buf=new Bufferedinputstream (in);


  7. Byte[]bytes=new byte[1024];

  8. Data read

  9. int len=-1;

  10. StringBuffer sb=new StringBuffer ();

  11. while ((Len=buf.read (bytes))!=-1)

  12. {

  13. Sb.append (New String (Bytes,0,len));

  14. }

  15. System.out.println ("The content is:" +SB);

  16. Close the stream

  17. Buf.close ();

  18. In.close ();



  19. } catch (IOException e) {

  20. E.printstacktrace ();

  21. }


  22. }

Copy Code

Operation Result:
650) this.width=650; "id=" aimg_301 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132834h6frj37j87jrzff8.png "class=" Zoom "width=" 301 "alt=" 132834h6frj37j87jrzff8.png "/>

Two. Character Buffer stream

1.bufferedwriter-character output Buffer stream

Writes text to the character output stream, buffering individual characters, providing efficient writes. You can specify the size of the buffer, which, in general, is sufficient for the default buffer size.

650) this.width=650; "id=" aimg_302 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132856uylv2mv2s2jw2m5m.png "class=" Zoom "width=" 538 "alt=" 132856uylv2mv2s2jw2m5m.png "/>

Example code:

  1. public static void Main (string[] args) {


  2. try {

  3. Writer w=new FileWriter ("L:\\test.txt");

  4. To create a character buffer stream from a character output stream

  5. BufferedWriter buf=new BufferedWriter (w);

  6. Write Data

  7. Buf.write ("Kong Mulberry becomes Satin");

  8. Refresh Stream

  9. Buf.flush ();

  10. Close the stream

  11. Buf.close ();

  12. W.close ();

  13. } catch (IOException e) {

  14. E.printstacktrace ();

  15. }



  16. }

Copy Code

Operation Result:
650) this.width=650; "id=" aimg_303 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132925n4jq22bbqc12bzjk.png "class=" Zoom "width=" 499 "alt=" 132925n4jq22bbqc12bzjk.png "/>

2.bufferedreader-character input buffer stream

Reads information from the character input stream, buffering individual characters for efficient reading. You can specify the size of the buffer, which, in general, is sufficient for the default buffer size. The default size is 8192.


Example code:

  1. public static void Main (string[] args) {


  2. try {

  3. Reader r=new FileReader ("L:\\test.txt");

  4. To create a character buffer stream from a character input stream

  5. BufferedReader buf=new BufferedReader (R);


  6. Char [] data=new char[512];

  7. Data read

  8. int len=-1;

  9. StringBuilder sb=new StringBuilder ();

  10. while ((Len=buf.read (data))!=-1)

  11. {

  12. Sb.append (New String (Data,0,len));

  13. }

  14. System.out.println ("Content is:" +SB);

  15. Close the stream

  16. Buf.close ();

  17. R.close ();

  18. } catch (IOException e) {

  19. E.printstacktrace ();

  20. }



  21. }

Copy Code

Operation Result:
650) this.width=650; "id=" aimg_304 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132957vzfact8cchsct8xv.png "class=" Zoom "width=" 317 "alt=" 132957vzfact8cchsct8xv.png "/>


"Buffer stream of Io stream for Java"

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.