The concept analysis of Stream stream in Java and its practical application summary _java

Source: Internet
Author: User
Tags textout wrapper

A stream is an abstract concept of a sequence of bytes.
The file is the static storage form of the data, and the stream refers to the form of transmission.
The flow class is divided into two categories: node stream class and Filter flow class (also called process Flow Class).
The program is used to directly manipulate the target device's corresponding class called Node Stream class. The program can also invoke the node stream class through an indirect stream class to achieve more flexible and convenient reading of various types of data, which is the filter flow class (also known as the processing Stream class), or the wrapper class.
The calling procedure for the wrapper class is shown below:

The relationship of flow classification
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:

Word throttling Character streams
Input stream InputStream Reader
Output stream OutputStream Writer

In Java, the character is stored in Unicode encoding, and the character stream processing class is responsible for converting other encoded character streams from outside to the stream of Unicode characters in Java. The class InputStreamReader and OutputStreamWriter handle the conversion of character streams and byte streams. Character streams (one buffer at a time) is more efficient than a byte stream (one bytes at a time).

InputStream

Since InputStream and OutputStream are abstact classes, they do not indicate exactly which IO device to correspond to. They have many subclasses, including networks, pipelines, memory, files, and other specific IO devices, and their various subclass objects used in the actual program.
Note: We refer to the IO source and target of the node stream class as a stream node.
Note: Write the contents of a file to the B file, the program to the operation of a file is the output class or input class this problem. The input output class is relative to the program, not the file, so we should create an input class to complete the operation of a file, create an output class to complete the operation of the B file.

OutputStream

Character-oriented stream Reader/writer
A Unicode-oriented stream that writes information from a stream to read from or to a stream in a Unicode character. Similarly, Reader/writer is also a abstact class.

Reader

Writer

IO Program code reuse:
Usually write code with--as the end of keyboard input, System.in is not directly used in the written function. Just call the function, the system.in as a parameter passed in, so that we want to read data from a file in lieu of manual keyboard input, we can directly use this function, the program does not have to do too much modification, to achieve status quo effect.

The conversion between byte stream and character streams
InputStreamReader and Outputstreamreader: Converts a byte-oriented stream into a character-oriented stream.
The InputStreamReader class is a bridge from byte stream to character streams: it reads bytes and converts them to character streams according to the specified encoding.
The encoding used may be specified by name, or by the default encoding that the platform accepts.
Each invocation of one of the InputStreamReader read () methods may cause one or more bytes to be read from the base byte input stream.
To achieve greater efficiency, consider using the BufferedReader package InputStreamReader,

BufferedReader in = new BufferedReader (new InputStreamReader (system.in));

A summary of Java streaming usage
Most of the work encountered a lot of Java flow use, summed up as follows:
1. Generate the ZIP format, encountered is to generate a zip file in a servlet, output to the Web client, direct download.

Response.setcontenttype ("Application/zip"); 
 Response.AddHeader ("Content-disposition", "attachment;filename=/" xxx.zip/")"; 
 Zipoutputstream out = new Zipoutputstream (Response.getoutputstream ()) for 
  
 () 
 {  
  ZipEntry entry = new ZipEntry ("AA" + i ". Dat"); 
  Out.putnewentry (entry); 
  bytes[] bt = S.getbytes (); 
  Out.writebytes (BT, 0, Bt.length ()); 
  Out.closeentry (); 
 } 
 Out.flush (); 
 Out.close (); 

Zipoutputstream inherits from Java.io.FilterOutputStream. So the real write operation is written by the parameter outputstream out.
Its void write (byte[] b, int off, int len) finally calls Out.write (b, off, Len);

If you want to generate a zip file, write new Zipoutputstream (new FileOutputStream path) as you construct it;

2. Write XML like that.

 XMLWriter writer = new XMLWriter (new FileOutputStream (path), Formater)

Writer.write (DOC). The truth is similar to the above
3. Write text file, append.

PrintStream PS = new PrintStream (new FileOutputStream (path, True), "Utf-8")

ps.println (s);//Can write Boolean, int, and all types.

Printsteam also inherits from Filteroutputstream
It is written internally using a OutputStreamWriter object textout. For example, write (String s) is eventually called to Textout.write (s);

This involves coding the problem. The "Utf-8" in its parameters is eventually passed to OutputStream.

OutputStream is a bridge between character streams and byte streams.
Thus it provides write (char[] cbuf, int off, int len) and write (string str, int off, int len) for writing characters and strings.
The OutputStream also serializes characters and strings internally with a Streamencoder object.
4. Write to socket.
 DataOutputStream out = new DataOutputStream (Socket.getoutputstream ());
 Out.writebytes (BT);
 Out.writeboolean (Boolean v); 

DataOutputStream is also a self filteroutputstream.

5. Read from the text

 BufferedReader reader = new BufferedReader (new FileReader (path));
 Reader.readline ();

The BufferedReader pattern, like the filter mode above, stores an object whose reader object is passed in as a parameter and is actually read.
BufferedReader corresponds to Java 1.0 class is Bufferedinputstream, is a filterinputstream.

6. Read from the socket

 Bufferedinputstream is = new Bufferedinputstream (Socket.getinputstream ());
 Is.read (BT, 0, Bt.length ());


Summary:
the base stream series are InputStream and OutputStream, they are abstract classes, and the required method is only (for example, in output)

void write (int b) throws IOException;
void write (byte b[]) throws IOException
void Write (byte b[], int off, int len)

Its most basic is the byte operation. The 1th method seems to write an integer, but only one byte (minimum eight bit) is written. Its subclasses are divided into two series, one is the direct operation of the output device, we encountered above the file (fileoutputstream) and servlet output (Servletoutputstream). Other commonly used also has a bytearrayoutputstream, is directly in the memory operation.
And then Filteroutputstream series, are receiving a OutputStream object seat parameters, the real write operation through the object to complete. For example, the Zipoutputstream itself is responsible only for generating data in compressed format, as to whether the data is written to a file, memory, or servletresponse, determined by the input parameters. This is the adorner pattern.

The filter series is commonly used with PrintStream (Print,println,write (Boolean[int, char, String) operations, which are eventually written in a writing section using the Out.write method.
There are also DataOutputStream, which provide Writebyte/writeboolean/writedouble/writelong/wiretutf and other methods.
There is socket/zip and so on is not commonly used.

Java flows are convenient and complex. Complexity is complex in implementing a function that often requires multiple classes, and there are multiple combinations of approaches. It still needs to be summarized in practice.

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.