Flow
First, the basic concept
The operation of the file in Java is done in a streaming fashion. A stream is an ordered set of data sequences in Java memory. Java data from source (file, memory, keyboard, network)
Reads into memory, forms a stream, and then writes these streams to another destination (file, memory, console, network), which is called a stream because the data sequence operates at different points in different parts of the source.
Ii. Classification of Streams
Stream classification, Java Stream classification is rich, just contact people will feel dizzy after seeing. There are many ways to categorize streams:
1, according to the direction of input, input stream and output stream, the input and output of the reference object is a Java program.
2, according to the processing data units different points, byte stream and character stream, the byte stream read the smallest unit is a bytes (1byte=8bit), and the character stream can read one character at a time (1char = 2byte = 16bit).
3, according to the different points of the function, sub-node flow and processing flow, the node flow is directly from a source read and write data stream (this stream has not been packaged and modified), the processing flow is on the basis of the node flow encapsulation, a flow, FileInputStream
is a contact flow that can read data directly from a file, but the Bufferedinputstream can be packaged with FileInputStream, making it a buffer function.
In fact, in addition to the above three categories, there are some commonly heard some categories such as: Object flow, buffer stream, compressed stream, file flow and so on. is actually a sub-classification of node flows and processing streams. Of course you can also create new stream types as long as you need.
Third, the relationship
Regardless of how rich and complex the classification of the stream is, its roots come from four basic classes. The relationship between the four classes is as follows:
Stream of Byte stream characters
Input stream InputStream Reader
Output stream OutputStream Writer
<!--endfragment-->
Iv. conversion of byte-stream and character stream
1, from byte stream to character streams: InputStreamReader, OutputStreamWriter class can be implemented.
2, from character flow to byte stream: You can get char[] array from the character stream, convert to string, then call the API function of String GetBytes () get to byte[], and then you can pass Bytearrayinputstream,
Bytearrayoutputstream to convert to a byte stream.
V: Example
Additional: In addition to using InputStreamReader and writer to convert a stream to a string unexpectedly, you can:
Inputsteaminch=.. Bytearrayoutputstream OutputStream=NewBytearrayoutputstream ();byte[] Data=New byte[1024x768];intlen=0; while((len=inch. Read (buffer))!=-1) {outputstream.write (data,0, Len);}byte[]data_b=outputstream.tobytearray ();//convert to byte arrayString data_s=NewString (data,"Utf-8");//Convert to String
1: reading data through streams
..... FileInputStreaminch=openfileinput ("file name")//InputStream In=connection.getinputstream ();//read file and network returned dataInputStreaminch=Xx.getinputstream (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (inch));//Convert bytes to characters using InputStreamReader while(Reader.readline ()! =NULL) {StringBuilder sb=NewStringBuilder ();//Higher assembly efficiency with StringBuilderSb.append (Reader.readline ());} ...
2: Write data by stream
out =openfileoutput (" file name ", file operation mode); BufferedWriter writer=new bufferedwriter (new outputstreamwriter (out)); // first converted to a character type when the buffered character output stream is called writer.write (data), .....
Java flow Rollup and usage instances