Understanding and use of filechannel class
Understanding and use of filechannel class (Java. NiO. Channels. Filechannel)
Knowledge points:
1. Filechannel class and method understanding; 2. Common input / output stream copy file; 3. Copy files through filechannel; 4. New noun record: {mappedbytebuffer: directly replace the file mapped in memory with byte data; filelock: represents the lock of the file; ByteBuffer: cache object}
summary
For file copying, we usually use input and output streams to create an input stream using the source file, then create an output stream using the target file, and finally read and write the data of the input stream into the output stream. This can also be operated. But using filechannel is a very useful way. It can directly connect the file channel of input and output stream and write data directly to the target file. And more efficient.
Filechannel is a channel for reading, writing, mapping and operating a file. In addition to the read-write operations, there are functions such as cropping a file of a specific size, truncate (), forcing the data in memory to be refreshed to the hard disk, force (), locking the channel, lock (), and so on.
Their use is as follows:
Explanation: the above code first creates a 1024 size buffer object, then reads the 1024 size data in the input channel and puts it into the buffer object.
Explanation: the above code is to write a string to the output file channel. Because it is not guaranteed that it can be written to the file at one time, you need to judge whether to write it all. If not, you need to call the write function again
Explanation: the above code is to obtain the location and size of the file channel. The truncate () method intercepts 1024 size data, and the part after the specified length will be deleted. And forcibly refresh the data to the hard disk. Because the system will save the data in the memory first, it is not guaranteed that the data will be written to the hard disk immediately. Therefore, if there is this requirement, you can directly force the data to be written to the memory.
It may be useless to say so much. Let's just take a look at the comparison between the two methods of file replication.
The first is to copy files from ordinary I / O streams:
Explanation: in the above code, the source file and the target file are passed in, and then the input and output streams are issued respectively according to the two files. Then, the data of the input stream is read and written into the output stream, and the file copy operation is completed.
Let's take another look at the file replication operation using filechannel.
Explanation: in the above code, the input and output streams of two files are created respectively, and then the file channels of the two files are obtained respectively. Then, the file channel of the source file is directly connected with the file channel of the target file, and the data is directly written to the area in the target file. There is no need for separate read and write operations.
After running the code, copy a file. Comparing the two replication methods, it is found that the time used by filechannel is nearly half shorter than that of ordinary reading and input. Especially when copying large files, filechannel has more advantages. No pictures will be posted here. Excuse me!
summary
Here we know the filechannel class and its features and functions, so we can make good use of it. Especially when we copy files, we can make better use of this class, improve efficiency, and prevent other situations such as oom.
The above is all the content. If you have any questions, please leave a message and contact me in time. (blog:)