A Java NIO FileChannel is a channel which is connected to a file. Using A file channel you can read the data from a file, and write the data to a file. The Java NiO FileChannel class is NiO ' s a alternative to reading files with the standard Java IO API.
A FileChannel cannot is set into non-blocking mode. It always runs in blocking mode.
Opening a FileChannel
Before can use a filechannel you must open it. You cannot open a filechannel directly. You need to obtain a filechannel via an inputstream, OutputStream, or a randomaccessfile. Here's how you open a FileChannel via a randomaccessfile:
Randomaccessfile afile new randomaccessfile ("Data/nio-data.txt", "RW"); FileChannel = Afile.getchannel ();
Reading Data from a FileChannel
To read data from a filechannel one of the read() methods. Here are an example:
Bytebuffer buf = bytebuffer.allocate (); int bytesread = Inchannel.read (BUF);
First a Buffer is allocated. The data read from the FileChannel are read into the Buffer.
Second the Filechannel.read () method is called. This method reads data from the FileChannel into the Buffer. The int returned by the read () method tells how many bytes were witten into the Buffer. If-1 is returned, the End-of-file is reached.
Writing Data to a FileChannel
Writing data to a filechannel are done using the Filechannel.write () method, which takes a Buffer as parameter. Here are an example:
String NewData = "New string to write to file ..." += bytebuffer.allocate ($); Buf.clear (); Buf.put ( Newdata.getbytes ()); Buf.flip (); while (Buf.hasremaining ()) { channel.write (BUF);}
Notice how the Filechannel.write () method is called inside a while-loop. There is no guarantee of what many bytes the write () method writes to the FileChannel . Therefore We repeat the write () Call until the Buffer have no further bytes to write.
Closing a FileChannel
When you were done using a filechannel must close it. Here's how it's done:
Channel.close ();
FileChannel Position
When the reading or writing to a filechannel a specific position. You can obtain the current position of the FileChannel object by calling the position () method.
You can also set the position of the FileChannel by calling the position (long Pos) method.
Here is the examples:
Long+123);
If you set the position after the end of the file, and try to read from the channel, you'll get-1-the End-of-file Mar Ker.
If you set the position after the end of the file, and write to the channel, the file is expanded to fit the position and written data. This could result in a "file hole", where the physical file is on the disk with gaps in the written data.
FileChannel Size
The size () method of the FileChannel object returns the file size of the file the channel is connected to. Here are a simple example:
long fileSize = Channel.size ();
FileChannel Truncate
You can truncate a file by calling the Filechannel.truncate () method. When you truncate a file, you cut it off at a given length. Here are an example:
Channel.truncate (1024);
This example truncates the file at 1024x768 bytes in length.
FileChannel Force
The Filechannel.force () method flushes all unwritten data from the channel to the disk. An operating system could cache data in memory for performance reasons, so is not guaranteed that data written to the C Hannel is actually-written to disk, until-the force () method.
The Force () method takes a Boolean as parameter, telling whether the file meta data (permission etc) should is flushed to O.
Here's an example which flushes both data and meta data:
Channel.force (true);
Java NIO FileChannel