FileChannel文件通道
FileChannel文件通道
打开文件通道(Opening a FileChannel)
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();从文件通道内读取数据(Reading Data from a FileChannel)
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf);向文件通道写入数据(Writing Data to a FileChannel)
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);
}关闭通道(Closing a FileChannel)
FileChannel Position
FileChannel Size
FileChannel Truncate
FileChannel Force
Last updated