File replication of IO stream in Java

O (∩ ∩) O ha ha~

1. Overview

A mature language must have several modules: IO, communication, thread, UI

As a mature programming language, Java's IO flow is relatively complex. In the previous picture, let's feel:

For a brief analysis, IO is divided into two streams: character stream and byte stream. The parent classes of character stream are reader (read to memory) and writer (output from memory), and the parent classes of byte stream are InputStream (read to memory) and OutputStream (output from memory), and then in order to facilitate various operations, such as deriving the file stream for file operation; deriving the object stream for object operation; and so on. At first, I was stupid and couldn't tell whether it was input or output. In fact, it was easy to understand who was the subject. Taking the program you are writing as the subject, input is flowing into your program, and output is from Your program flows out.

2. Understanding of buffer

When I first learned IO, I didn't understand the function of buffered. Why should I / O always have a buffer transition? Later, I read Baidu and knew that it was written by the last God. I personally think it makes sense. Copy and paste as follows. It should not be an infringement (⊙⊙)

"If you read and write at the same time, it will be very slow and hurt the hard disk. The buffer is an area in the memory. The data is stored in the memory first and then written at one time, which is similar to the batch operation of the database. This is more efficient. When calling I \ O operations, you actually read or write one by one. The key is that there is only one CPU, no matter how many cores. The CPU is in the system Will you participate in the main operation when calling? More participation will take more time. When the system calls, if no buffer is used, the CPU will consider using interrupts as appropriate. At this time, the CPU is active and spends part of each cycle asking whether the I \ O device has read the data, The CPU can't do anything else during this time (at least the core responsible for executing this module cannot). Therefore, the call reads one word at a time, notifies it once, and the CPU frees up time for processing once. When setting the buffer, the CPU usually uses DMA to perform I \ O operations. The CPU gives this work to the DMA controller and makes time for other things. When DMA completes its work, DMA will actively tell the CPU that the operation is complete 。 At this point, the CPU takes over the follow-up work. Here, the CPU is passive. DMA is dedicated to I \ O and memory data exchange. It not only has high efficiency, but also saves CPU time. CPU makes some settings at the beginning and end of DMA. So, call once without notifying the CPU. When the buffer is full, DMA will say "Hey, man! Come and have a look and move them away". To sum up, setting the buffer creates a data block, which makes DMA execution more convenient, and the CPU is free instead of waiting for I \ O data to be read. From a microscopic point of view, setting buffer efficiency is much higher. Although, it can't be seen from this program. Tens of thousands of words of reading and writing can see the gap. "

OK, as can be seen from the above, it saves time and effort.

3. Document copying

The principle of file copying is very simple. Reading the file from the hard disk to the program, and then outputting the file from the program to the target file completes the file copying. In fact, the file input and output stream in Java is used. Looking at a lot of code, I actually ignore those try catch and exception handling. It's a few lines of code.

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
分享
二维码
< <上一篇
下一篇>>