The abstract representation of different input/output sources (keyboards, files, network connections, etc.) in Java is Stream (stream).
Java programs can access different input/output sources by using different streams. Stream (stream) can be intuitively understood as such an ordered piece of data between the source of the data and the receiving (Sink) of the data.
PS. Note this is stream (stream) not Scream (cream) in ice cream
As you can see by reference, in your mind, the water droplets on the way are the data in the stream, and they collectively form this orderly data (flow).
First, the classification of the flow
1, according to the direction of the flow (from the memory of the program running in the perspective of the Division)
Input stream: Data can only be read from the stream and cannot be written out to it. (base class InputStream, Reader)
Output stream: You can only write data to it and cannot read data from it (base class Outputstream,writer)
2, according to the operation of the data unit is different
BYTE stream: The smallest data unit of the operation is bytes (8 bit), mainly InputStream, Outputsteam as the base class
Character stream: the smallest data unit of the operation is the characters (16bit), mainly in reader, writer as the base class
3, according to the role of the flow
Node Flow: A stream that can read and write data from/to a specific IO device. This flow belongs to a low-level stream.
This flow is often connected directly to the actual (anti-theft connection: This article is starting from http://www.cnblogs.com/jilodream/) data source, which is directly connected to the actual input and output nodes.
Process flow (wrapper flow): Used to connect and encapsulate an already existing stream and to read and write the data through the encapsulated stream. This flow tends to become an advanced stream.
This flow is wrapped around different node streams, allowing the program to access different data sources using exactly the same input and output code.
is a typical appearance pattern. That is, the use and inheritance of the original class are not changed. Wrap the real data by creating a wrapper object. The use and inheritance of the original class here is the node stream. Package streams to wrap real data, while extending functionality.
The conceptual model of flow
Flow can be understood as: the input device is abstracted into a "water pipe", the water pipe each "droplet" sequence, each droplet can be understood as the input and output units (that is, the bytes and characters mentioned in the previous article). These droplets form an ordered sequence of flows.
The input stream uses an implicit record pointer to indicate which water droplet is currently being prepared to begin reading the data. The recording pointer automatically moves backwards whenever the program pulls one or more "drops" from the inputstream/reader.
The output stream can be understood as a pipe that has reached the target, and the program will prevent the output "water droplets" from being piped to the output.
This allows us to understand that the stream consumes the resources of the operating system, in addition to allocating space in memory.
Let's look at an example.
1 Public classTempstreammodle2 {3 Public Static voidMain (string[] args)4 {5PrintStream ps=NULL;6 Try7 {8ps=NewPrintStream (NewFileOutputStream ("OUT.txt"));//Create an output stream9System.setout (PS);//assigns the output stream to the PS streamTenSystem.out.println ("Common String"); OneSystem.out.println (NewTempstreammodle ()); A } - Catch(IOException ex) - { the //Solve Error - } - finally - { + if(ps!=NULL) - { + ps.close (); A } at } - } -}
Java's GC can only reclaim objects that are not used in the JVM, and other resources assigned to the operating system cannot be reclaimed. So we need to call Close () on our own initiative.
The benefits of calling the close () method are:
(1) Ensure that the physical resources occupied by the stream can be released
(2) Flush (flush) The data in the output stream buffer to the physical node. Equivalent to actively executing the flush () method.
PS. java when using a stream, generally there is a buffer, in a way it thinks more efficient method to send data: The application every IO to communicate with the device, the efficiency is very low, so the buffer for efficiency, when writing to the device, the buffer will be written, until the buffer has enough data ( Anti-theft Connection: In this article starting from http://www.cnblogs.com/jilodream/), the overall write device (note that when drawing GUI interface, it is also used to ensure that the interface is not flashing when drawing or dragging). Flush () means that the data in the buffer is forced to be sent out without waiting until the buffer is full to execute. So if you don't use the flush () method when using a stream, there are many situations where the other side of the stream cannot read the data, especially if the data is very small.
This is equivalent to the tourist attractions in the tourism car, people will start (flush), but when the end of the day, no matter how many people in the last bus, will send the last bus, and not wait until the car full (buffer full).
Unreleased Resource (resource not freed)-streams (stream)