Java nio: The FileChannel of the NIO series tutorial in Java

Source: Internet
Author: User

One: Java Nio's FileChannel
The FileChannel in ===>java NiO is a channel connected to a File. You can read and write files through a file channel.

===>filechannel cannot be set to non-blocking mode, it is always running in blocking Mode.




Ii: Opening of Java Nio's FileChannel

===> before you can use filechannel, you must first open it. however, We cannot directly open a filechannel and need to obtain a FileChannel instance by using a inputstream, outputstream, or randomaccessfile.

The following is an example of opening FileChannel via Randomaccessfile:

1 Randomaccessfile afile = new Randomaccessfile ("data/nio-data.txt", "rw");
2 FileChannel inchannel = Afile.getchannel ();


Three: FileChannel of Java NiO reads data from FileChannel

Calls one of several read () methods to read data from the Filechannel. Such as:

1 Bytebuffer buf = bytebuffer.allocate (48);
2 int bytesread = Inchannel.read (buf);
first, Assign a Buffer. The data read from the FileChannel will be read into Buffer.

then, Call the Filechannel.read () method. This method reads the data from the FileChannel into Buffer. The int value returned by the Read () method indicates how many bytes were read into Buffer. If 1 is returned, the end of the file is Indicated.


Four: FileChannel of Java NiO writes data to FileChannel


Write the data to FileChannel using the Filechannel.write () method, which is a buffer. Such as:

NewData string = "New string to write to file ..." + system.currenttimemillis ();
02
Bytebuffer buf = bytebuffer.allocate (48);
Buf.clear ();
Buf.put (newdata.getbytes ());
06
Buf.flip ();
08
While (buf.hasremaining ()) {
Ten Channel.write (buf);
11}
Note that Filechannel.write () is called in the while Loop. Because there is no guarantee that the write () method can write many bytes to FileChannel at one time, the write () method needs to be called repeatedly until no bytes have been written to the channel in Buffer.



V: FileChannel shutdown of Java NiO FileChannel

You must close the FileChannel after you have finished using it. Such as:

1 Channel.close ();
The position method of FileChannel

Sometimes it may be necessary to read/write the data at a specific location in the Filechannel. You can get the current position of the FileChannel by calling the position () Method.

You can also set the current position of FileChannel by calling the position (long POS) method.

Here are two examples:

1 Long pos = channel.position ();
2 channel.position (pos +123);
If you set the location after the file terminator and then try to read the data from the file channel, The Read method returns the -1--file end Flag.

If you set the location after the file terminator and then write the data to the channel, the file will brace up to the current location and write the Data. This can result in a "file hole" where there is a gap between the data written in the physical file on the Disk.





Vi: the size method of the FileChannel FileChannel of Java NiO

The size () method of the FileChannel instance returns the sizes of the files associated with the Instance. Such as:

1 long fileSize = Channel.size ();




Vii: the Truncate method of the FileChannel FileChannel of Java NiO

You can use the Filechannel.truncate () method to intercept a file. When a file is intercepted, the portion of the file that follows the specified length is Deleted. Such as:

1 Channel.truncate (1024);
This example intercepts the first 1024 bytes of a file.





Viii: the Force method of Java Nio's FileChannel FileChannel

The Filechannel.force () method forces the data that has not been written to disk in the channel to be written to Disk. For performance reasons, the operating system caches data in memory, so there is no guarantee that data written to FileChannel will be written to disk Immediately. To ensure this, the force () method needs to be Called.

The Force () method has a Boolean parameter that indicates whether the file metadata (permission information, and so On) is written to disk at the same time.

The following example forces file data and metadata to be written to disk at the same time:

1 Channel.force (true);





Java nio: The FileChannel of the NIO series tutorial in Java

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.