How to understand the stream concept in Java

zhaozj2021-02-12  146

The flow mechanism is an important mechanism in Java and C , which allows us to freely control the flow of data in files, memory, IO devices, and the like. Such as: You can obtain data from the file input stream. After processing, the data is output to the network device through the network output stream; or use the object output stream to output the object in a program into a format stream file, and via the network Stream objects are output to the remote machine and then restore objects to the object input stream on the remote machine. Like these mechanisms are unqualified by other advanced languages. But to master these streaming objects, the concept of stream is very important.

The stream is a collection of continuously continuous data, just like the water flow in the water pipe, water supply at one end of the water pipe, while the other end of the water pipe is seen in a continuous water flow. The data write program can be written to data in a segment to the data stream, which will form a long data stream in the first order. For data read programs, you can't see the segmentation case when the data stream is written. You can read the data of any length each time, but you can only read the previous data before reading the back. The data. Regardless of writing, the data is written multiple times or as a whole, the effect is exactly the same.

The stream in Java can be divided into input streams and output flows in Java, which can be divided into node streams and packages.

Input stream, the output stream is based on the program as a reference point. The so-called input stream is the stream of the program from which the data is acquired, the output stream is the stream of the program to write data. On the side of the input stream is the program, and the other is the stream data source. And the target is the target, while the program is the program. In fact, the stream can imagine a long river, there is a reservoir in the upstream, there is a water source, and the river lives in a house, and you can take the water from the river. At the same time, this household can also pour some wastewater into the river, make Waste water can flow into the sea. The river mentioned here is a flow channel of a data, and the reservoir is like all data sources on the computer, including disk files, memory, IO devices, keyboards, etc., Java provides a very complete input stream to put these data. Source-mounted stream allows programs to get the required data from these input streams. The user on the river is the program, which can obtain data from the stream anytime, anywhere, as long as there is a data source to be connected to this channel. The sea is the destination to flow after the program is processed, these destination encloses disk files, memory, IO devices, displays, etc. These destinations are just relative programs, which may also be other process input streams. .

As such example, output data to another file, then read data from the file

FILE F = New File ("1. Test");

Try

{

FileOutputStream out = new fileoutstream (f);

BYTE BUF [] = "Hello World" .getbytes ();

Out.write (buf);

Out.close ();

}

Catch (Exception E)

{

System.out.println (E.getMessage ());

}

Try

{FileInputstream in = new fileinputstream (f);

BYTE BUF [] = New byte [1024];

INT LEN = In.read (buf);

System.out.println (New String (BUF, 0, LEN));

}

Catch (Exception E)

{System.out.println (E.getMessage ());

}

The program is used to directly operate the keycarcatal stream class corresponding to the target device, and the program can also call the node stream through a indirect stream to achieve more flexible and convenient reading and writing of various types of data. This indirect stream is packaged. Flow. In Java

in

转载请注明原文地址:https://www.9cbs.com/read-7675.html

New Post(0)