Java NIO FileChannel
2016-10-27 20:39
246 查看
files with the standard Java IO API.
A
FileChannelcannot be set into non-blocking mode. It always runs in blocking mode.
Opening a FileChannel
Before you can use a FileChannelyou must open it. You cannot open a FileChannel directly. You need to obtain a FileChannel via an InputStream, OutputStream, or a RandomAccessFile. Here is how you open a FileChannel via a
RandomAccessFile:
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw"); FileChannel inChannel = aFile.getChannel();
Reading Data from a FileChannel
To read data from a FileChannelyou call one of the
read()methods. Here is an example:
ByteBuffer buf = ByteBuffer.allocate(48); int bytesRead = inChannel.read(buf);
First a
Bufferis allocated. The data read from the
FileChannelis read into the
Buffer.
Second the
FileChannel.read()method is called. This method reads data from the
FileChannelinto the
Buffer. The
intreturned
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 FileChannelis done using the
FileChannel.write()method, which takes a
Bufferas parameter. Here is an example:
String newData = "New String to write to file..." + System.currentTimeMillis(); ByteBuffer buf = ByteBuffer.allocate(48); 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 how many bytes the
write()method writes to the
FileChannel.Therefore
we repeat the
write()call until the
Bufferhas no further bytes to write.
Closing a FileChannel
When you are done using a FileChannelyou must close it. Here is how that is done:
channel.close();
FileChannel Position
When reading or writing to a FileChannelyou do so at a specific position. You can obtain the current position of the
FileChannelobject by calling the
position()method.
You can also set the position of the
FileChannelby calling the
position(long pos)method.
Here are two examples:
long pos channel.position(); channel.position(pos +123);
If you set the position after the end of the file, and try to read from the channel, you will get -1 - the end-of-file marker.
If you set the position after the end of the file, and write to the channel, the file will be expanded to fit the position and written data. This may result in a "file hole", where the physical file on the disk has gaps in the written data.
FileChannel Size
The size()method of the
FileChannelobject returns the file size of the file the channel is connected to. Here is 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 is an example:
channel.truncate(1024);
This example truncates the file at 1024 bytes in length.
FileChannel Force
The FileChannel.force()method flushes all unwritten data from the channel to the disk. An operating system may cache data in memory for performance reasons, so you are not guaranteed that data written to the channel is actually
written to disk, until you call the
force()method.
The
force()method takes a boolean as parameter, telling whether the file meta data (permission etc.) should be flushed too.
Here is an example which flushes both data and meta data:
channel.force(true);
相关文章推荐
- java.nio.channels.FileChannel 使用笔记
- Java Nio 八、Java NIO FileChannel
- Java - NIO之Channel(FileChannel)
- 08. Java NIO FileChannel 文件通道
- Java NIO FileChannel
- Java NIO 学习(三)--FileChannel
- Java NIO AsynchronousFileChannel
- java.nio.channels.FileChannel文件通道源码解析
- Java的NIO之FileChannel
- JAVA NIO FileChannel 内存映射文件
- 【原创】java NIO FileChannel 学习笔记 FileChannel实现分析 即FileChannelImpl分析
- FileChannel类的理解和使用(java.nio.channels.FileChannel)
- 【JAVA】【NIO】8、Java NIO FileChannel
- java NIO系列: Java NIO AsynchronousFileChannel
- JavaNio——FileChannel
- 【原创】java NIO FileChannel 学习笔记 FileChannel 简介
- JAVA NIO之FileChannel
- Java NIO FileChannel
- Java NIO FileChannel
- 【原创】java NIO FileChannel 学习笔记 新建一个FileChannel