"Buffer stream of Io stream for Java"

Source: Internet
Author: User

The Java Buffer stream itself does not have IO functionality, but adds buffering to 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 streamsthe byte buffer stream is:bufferedinputstream-byte input buffer streambufferedoutputstream-byte output buffered streamthe character Buffer stream is:bufferedreader-character input buffer streambufferedwriter-character output buffer streamThe following mainly describes the use of these four kinds of buffer streams.
A. Byte buffer stream1.bufferedoutputstream-byte output buffered streamThe 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: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 Code2.bufferedinputstream-byte input buffer streamBufferedinputstream adds buffering capabilities to other input streams, creating an internal buffer array to buffer the data and improve performance when creating Bufferedinputstream. 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 CodeOperation Result:
<ignore_js_op>

two. Character Buffer stream1.bufferedwriter-character output buffer streamwrites 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. 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 CodeOperation Result:
<ignore_js_op>
2.bufferedreader-character input buffer streamreads 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 CodeOperation Result:
<ignore_js_op>

"Buffer stream of Io stream for Java"

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.