更多 Java IO & NIO方面的文章,請參見文集《Java IO & NIO》
- Instances of this class support both reading and writing to a random access file.
支持對文件的隨機訪問及讀寫操作。 - A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer;
有一個游標或者索引 file pointer,來訪問字節(jié)數(shù)組 -
getFilePointer()
得到當前 file pointer -
seek(long pos)
修改當前 file pointer
RandomAccessFile 的使用:
public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("r.txt", "rw");
raf.writeUTF("你好");
System.out.println(raf.getFilePointer());
raf.seek(0);
String s = raf.readUTF();
System.out.println(s);
}
輸出:
8
你好