Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
Random access files (随机访问文件) 允许对文件内容进行非顺序或随机访问。要随机访问文件,请打开文件,查找特定位置,然后读取或写入该文件。
使用 SeekableByteChannel 接口可以实现此功能。SeekableByteChannel 接口使用当前位置的概念继承通道 I/O。使用方法可以设置或查询位置,然后可以从该位置读取数据或将数据写入该位置。API 由一些易于使用的方法组成:
position–返回通道的当前位置position(long)–设置通道的位置read(ByteBuffer)–从通道读取缓冲区中的字节write(ByteBuffer)–将缓冲区中的字节写入通道truncate(long)–截断连接到通道的文件(或其他实体)
使用通道 I/O 读取和写入文件显示 Path.newByteChannel 方法返回 SeekableByteChannel 的实例。在默认文件系统上,你可以按原样使用该通道,也可以将其转换为 FileChannel,以便你访问更多高级功能,例如映射区域文件直接进入内存以便更快地访问,锁定文件的一个区域,或者从绝对位置读取和写入字节而不影响通道的当前位置。
以下代码段使用 newByteChannel 方法之一打开用于读取和写入的文件。返回的 SeekableByteChannel 将强制转换为 FileChannel。然后,从文件的开头读取 12 个字节,字符串“I was here!”在那个地方写入。文件中的当前位置移动到末尾,并附加从开头的 12 个字节。最后,字符串,“I was here!”附加上,并关闭文件上的通道。
String s = "I was here!\n";
byte data[] = s.getBytes();
ByteBuffer out = ByteBuffer.wrap(data);
ByteBuffer copy = ByteBuffer.allocate(12);
try (FileChannel fc = (FileChannel.open(file, READ, WRITE))) {
// Read the first 12
// bytes of the file.
int nread;
do {
nread = fc.read(copy);
} while (nread != -1 && copy.hasRemaining());
// Write "I was here!" at the beginning of the file.
fc.position(0);
while (out.hasRemaining())
fc.write(out);
out.rewind();
// Move to the end of the file. Copy the first 12 bytes to
// the end of the file. Then write "I was here!" again.
long length = fc.size();
fc.position(length-1);
copy.flip();
while (copy.hasRemaining())
fc.write(copy);
while (out.hasRemaining())
fc.write(out);
} catch (IOException x) {
System.out.println("I/O Exception: " + x);
}