The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Random access files permit nonsequential, or random, access to a file's contents. 随机访问文件允许对文件内容进行非顺序或随机访问。To access a file randomly, you open the file, seek a particular location, and read from or write to that file.要随机访问文件,请打开该文件,查找特定位置,然后读取或写入该文件。
This functionality is possible with the 此功能可通过SeekableByteChannel
interface. SeekableByteChannel
接口实现。The SeekableByteChannel
interface extends channel I/O with the notion of a current position. SeekableByteChannel
接口使用当前位置的概念扩展通道I/O。Methods enable you to set or query the position, and you can then read the data from, or write the data to, that location. 方法使您能够设置或查询位置,然后可以从该位置读取数据或将数据写入该位置。The API consists of a few, easy to use, methods:API由几个易于使用的方法组成:
position
– position(long)
– read(ByteBuffer)
– write(ByteBuffer)
– truncate(long)
– Reading and Writing Files With Channel I/O shows that the 使用通道I/O读取和写入文件表明Path.newByteChannel
methods return an instance of a SeekableByteChannel
. Path.newByteChannel
方法返回SeekableByteChannel
的实例。On the default file system, you can use that channel as is, or you can cast it to a 在默认文件系统上,您可以按原样使用该通道,也可以将其强制转换为允许您访问更高级功能的FileChannel
giving you access to more advanced features, such as mapping a region of the file directly into memory for faster access, locking a region of the file, or reading and writing bytes from an absolute location without affecting the channel's current position.FileChannel
,例如将文件区域直接映射到内存中以加快访问速度,锁定文件区域,或者从绝对位置读取和写入字节,而不影响通道的当前位置。
The following code snippet opens a file for both reading and writing by using one of the 下面的代码片段通过使用一个newByteChannel
methods. newByteChannel
方法打开一个文件进行读写。The 返回的SeekableByteChannel
that is returned is cast to a FileChannel
. SeekableByteChannel
被强制转换为FileChannel
。Then, 12 bytes are read from the beginning of the file, and the string "I was here!" is written at that location. 然后,从文件开头读取12个字节,并在该位置写入字符串“I was here!”。The current position in the file is moved to the end, and the 12 bytes from the beginning are appended. 将文件中的当前位置移到末尾,并追加从开头开始的12个字节。Finally, the string, "I was here!" is appended, and the channel on the file is closed.最后,附加字符串“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); }