API void setLength(long newLength) Sets the length of this file. (设置文件的预计大小) void seek(long pos) Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. 假设为这个方法传入 1028 这个参数,表示,将从文件的 1028 位置开始写入。 void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to this file. write(byte[] b) Writes b.length bytes from the specified byte array to this file, starting at the current file pointer. void writeUTF(String str) Writes a string to the file using modified UTF-8 encoding in a machine-independent manner. String readLine() Reads the next line of text from this file.
实验代码:
Java代码
public static void main(String[] args) throws Exception {
File file = new File("1.txt");
RandomAccessFile accessFile = new RandomAccessFile(file,"rwd");