Analysis of Java IO working mechanism

The IO classes of Java are all in Java Under the IO package, these classes can be roughly divided into the following four types:

1. Basic structure of IO class library

1.1 IO interface based on byte operation

IO interfaces based on byte operation are InputStream and OutputStream respectively. The class structure of InputStream is as follows:

Similar to InputStream, the OutputStream class has the same class structure diagram.

For the use of each subclass, please refer to the API documentation of JDK. Here we need to note that the methods of operating data can be combined, as shown below:

As can be seen from the above code, inputstreamreader can read data from FileInputStream. As can be seen from the source code, it is not only inputstreamreader, but all IO classes can be combined in this way.

Another thing to note is that you must determine where the stream will eventually be written, disk or network. As can be seen from the OutputStream class diagram, writing to the network is actually a file writing operation, but the bottom layer is transmitted through the network.

1.2 character based IO operation interface

Both disk and network data transmission are in bytes, but generally common data operations in programs are in characters (char in Java occupies 2 bytes and char in C / C + + occupies 1 byte). Therefore, we need to have an IO interface to operate characters to deal with the encoding conversion between characters and bytes, That is, write and reader interfaces and their implementation classes. Their class interface diagram is as follows:

The main operation method of the read character interface reader is read (), which reads characters and returns the number of characters read. Whether it is a writer or reader class, they only define the way to read or write data characters, that is, how to write or read, but do not specify where to write data (such as disk or network).

1.3 conversion interface between byte and character

Sometimes data persistence and network transmission are carried out in bytes, and all require conversion between bytes and characters.

Note: FileReader class inherits inputstreamreader. FileReader reads the file stream and decodes it into char through streamdecoder. Its decoded character set uses the default character set. In Java, we should use the file object to determine whether a file exists. If we open it with fileoutputstream or filewriter, it will be overwritten.

2 synchronous and asynchronous, blocking and non blocking

Synchronous and asynchronous are for Io. Synchronization means that when the completion of a task depends on another task, the dependent task can be completed only after the dependent task is completed. This is a reliable task sequence. Either success or failure, the status of the two tasks can be consistent. Asynchrony does not need to wait for the dependent task to complete. It just notifies the dependent task of what work to complete, and the dependent task will be executed immediately. As long as you complete the whole task, it will be completed. As for whether the dependent task is really completed in the end, the task that depends on it cannot be determined, so it is an unreliable task sequence. We can use calling and texting as a good metaphor for synchronous and asynchronous operations.

Blocking and non blocking are for CPUs. Blocking and non blocking mainly refer to the consumption of CPU. Blocking is that the CPU stops and waits for a slow operation to complete before the CPU completes other things. Non blocking means that the CPU does other things when the slow operation is executing. When the slow operation is completed, the CPU then completes the subsequent operations. On the surface, the non blocking method can significantly improve the utilization of CPU, but it also brings another consequence, that is, the thread switching of the system increases. Whether the increased CPU usage time can compensate the switching cost of the system needs to be evaluated.

3 serialization

Java object serialization converts those objects that implement the serializable interface into a byte sequence, and can completely restore this byte sequence to the original object in the future. This process can be carried out through the network, so that the serialization mechanism can automatically make up for the differences between different operating systems. The intelligence of corresponding serialization is that it not only saves the "panorama" of objects, but also can track the references contained by objects and save these objects; Then, each such reference contained in the object can be finally analyzed; and so on.

To instantiate an object, first create some OutputStream objects, and then encapsulate them in an objectoutputstream object. This is that you only need to call writeobject() to serialize the object and send it to OutputStream (object serialization is based on bytes, so InputStream and OutputStream inherit the class hierarchy). The reverse serialization and serialization process are just the opposite. You need to encapsulate a InputStream in ObjectInputStream, and then call readObject () to get a reference, which points to a Object that is upwardly transformed, so we must transform it downwards to set them directly. Here is the serialization and deserialization sample code:

reference material:

1. Deeply analyze the working mechanism of Java I / O

2. Java IO system

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>