"Java" "NiO" 8, Java NiO filechannel

Source: Internet
Author: User

Java NiO's FileChannel is a channel for connecting files. Through the file channel, you can read data from a file or write data to a file. Both the FileChannel class and the standard Java IO API are available to read files.

FileChannel cannot be set to non-blocking mode. It always runs in blocking mode.

Open File Channel

You must open the file channel before you use it. You cannot open the file channel directly. You need to get the file channel through Inputstream,outputstream or Randomaccessfile. As follows:

new RandomAccessFile("e:\\demo.txt""rw");FileChannel fromChannel = fromFile.getChannel();
Read data from a file channel

Through the Read method, the following:

ByteBuffer buffer = ByteBuffer.allocate(8);int bytesRead = channel.read(buffer);

First, a buffer is allocated, and the data read from FileChannel is read into buffer.
Second, call the Read method, read the data into buffer, and the int that the method returns shows how many bytes of data were written into buffer. If 1 is returned, the document is read.

Write data to a file channel

Through the Write method, the following:

Randomaccessfile fromfile = new Randomaccessfile ("E:\\demo.txt","RW");FileChannel channel = FromFile. Getchannel();Bytebuffer buffer = Bytebuffer. Allocate(Ten);Buffer. Clear();Buffer. Put("Hello". GetBytes());Buffer. Flip();while (buffer. hasremaining()) {Channel. Write(buffer);}fromfile. Close();

Note that the Write method is called within a loop and there is no guarantee as to how many bytes are written to the file channel, so we repeatedly call the Write method until buffer has no data.

Close File Channel
channel.close();
Position of file channels

You need to read and write to the specific location of the file channel, and you can get the current location of the file channel by calling the position method.
You can also set the position of a file channel by calling the position (long Pos) method. As follows:

pos = channel.position();channel.position(pos+2);

If you set the position after the end of the file, the read data from the channel will return-1.
Similarly, if you write, the file will grow, which may result in a hole in the file and a gap between the data written in the physical file on the disk.

Randomaccessfile fromfile = new Randomaccessfile ("E:\\demo.txt","RW");FileChannel channel = FromFile. Getchannel();Long fileSize = Channel. Size();System. out. println("====="+filesize);Long pos = Channel. Position();System. out. println("Current Position:"+ POS);Re-set Location channel. Position(pos+3);//File Hole formationBytebuffer buffer = Bytebuffer. Allocate(Ten);Buffer. Clear();Buffer. Put("a". GetBytes());Buffer. Flip();while (buffer. hasremaining()) {//write file Channel. Write(buffer);} buffer. Flip();int bytesread = Channel. Read(buffer);//Read Filewhile (bytesread!=-1) {Buffer. Flip();while (buffer. hasremaining()) {System. out. Print((char) buffer. Get());} System. out. println();Buffer. Clear();Bytesread = Channel. Read(buffer);} Channel. Close();FromFile. Close();



This picture of 20 is a space, and the above 00 is not the same, I would like to try this void is essentially not a space.



The above shows the file hole.

File Channel size

The size method returns the file sizes associated with the FileChannel. As follows:

long fileSize = channel.size();

Channel.size and File.length return the same result, both the size of the file and the number of bytes.

Truncate of file channels

intercepts files. When you intercept a file, you must specify a length. As follows:

channel.truncate(10);


If the given size is less than the current size of the file, the file is truncated and all bytes after the new end of the file are discarded. If the given size is greater than or equal to the current size of the file, the file is not modified . In either case, if the file location of this channel is greater than the given size, the position is set to that size.

Force of the file channel

The Force method flush all data that is not written to disk in the channel to disk. The operating system may cache data in memory for performance, so it is not guaranteed that the data written to the channel will be written to disk unless you call the Forece method.
The Force method has a Boolean parameter that indicates whether the file metadata (permissions, and so on) is written to disk. As follows:

channel.force(true);

Next section: Waiting

"Java" "NiO" 8, Java NiO filechannel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.