Java Core Class Library-io-wrapper flow overview and buffering flow principle

Source: Internet
Author: User
Tags wrapper

Process flow/wrapper flow (more advanced than node flow):

1. Hide the difference of the underlying node stream, and provide more convenient input/output function to the outside, let us care about the advanced flow Operation .

2. Using the processing packaging flow to wrap the node flow program directly manipulate the processing flow, so that the node flow and the underlying device to do IO operations.

3. Just close the process flow to

how to partition a wrapper stream: When you write code, you find that you need to pass another stream object when you create the object. New wrapper stream (Stream object);

What is a buffered stream:

is a wrapper stream for the purpose of caching the function.

Bufferedinputstream:

Bufferedoutputstream:

BufferedReader:

Bufferedwreiter:

Purpose of the buffered stream:

When manipulating the flow, it is customary to define a Byte/char array

int read (), each time a byte is read from the disk file, the direct manipulation of disk file performance is extremely low.

Solution: 11th Array as a buffer

byte[] buffer = new byte[1024]; The array is actually a buffer.

Read 1024 bytes from a disk file at once, so that the number of disk files has been manipulated and performance has been improved.

Sun provides a default buffer size of 8192 (1024*8), and we generally do not need to modify the size.

First read: Reads 8,192 bytes from the disk file into memory, and we read from memory into the program.

Each time the output is written out to the memory buffer, when the memory buffer size is full of 8,192 bytes, the buffer data is now written out to the disk file.

Byte buffer Stream (Bufferedinputstream,bufferedoutputstream)

1  Public Static voidMain (string[] args)throwsException {2         //byte buffered output stream3Bufferedoutputstream BOS =NewBufferedoutputstream (NewFileOutputStream ("Stream.txt",true));//true indicates that an append4Bos.write ("Hello word". GetBytes ());5 bos.close ();6 7         //byte buffer input stream8Bufferedinputstream bin =NewBufferedinputstream (NewFileInputStream ("Stream.txt"));9         byte[] buffer =New byte[1024];Ten         intLen =-1; One          while(len = bin.read (buffer))!=-1){ ASystem.out.println (NewString (buffer,0, Len)); -         } -Bin.close ();}

BufferedWriter and BufferedReader

 Public Static voidMain (string[] args)throwsIOException {//OutputBufferedWriter out =NewBufferedWriter (NewFileWriter ("Stream.txt",true)); Out.write ("Hoe wo Day Copse"); Out.newline ();//line BreakOut.write ("Sweat wo xia tu");        Out.close (); //inputBufferedReader in =NewBufferedReader (NewFileReader ("Stream.txt")); Char[] buffer =New Char[1024]; intLen =-1;  while(len = in.read (buffer))!=-1) {System.out.println (NewString (buffer,0, Len));        } in.close (); //read by rowBufferedReader in2 =NewBufferedReader (NewFileReader ("Stream.txt")); String Line=NULL;  while(line = In2.readline ())! =NULL) {System.out.println (line);    } in2.close (); }

Java Core Class Library-io-wrapper flow overview and buffering flow principle

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.