📜  Java RandomAccessFile

📅  最后修改于: 2020-09-27 07:38:44             🧑  作者: Mango

Java-RandomAccessFile

此类用于读取和写入随机访问文件。随机访问文件的行为类似于大字节数组。有一个游标隐含在称为文件指针的数组中,通过移动游标我们执行了读写操作。如果在读取所需的字节数之前到达文件末尾,则抛出EOFException。它是IOException的一种。

建设者

Constructor Description
RandomAccessFile(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
RandomAccessFile(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

方法

Modifier and Type Method Method
void close() It closes this random access file stream and releases any system resources associated with the stream.
FileChannel getChannel() It returns the unique FileChannel object associated with this file.
int readInt() It reads a signed 32-bit integer from this file.
String readUTF() It reads in a string from this file.
void seek(long pos) It sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
void writeDouble(double v) It converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first.
void writeFloat(float v) It converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the file as a four-byte quantity, high byte first.
void write(int b) It writes the specified byte to this file.
int read() It reads a byte of data from this file.
long length() It returns the length of this file.
void seek(long pos) It sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileExample {
static final String FILEPATH ="myFile.TXT";
public static void main(String[] args) {
try {
System.out.println(new String(readFromFile(FILEPATH, 0, 18)));
writeToFile(FILEPATH, "I love my country and my people", 31);
} catch (IOException e) {
e.printStackTrace();
}
}
private static byte[] readFromFile(String filePath, int position, int size)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(position);
byte[] bytes = new byte[size];
file.read(bytes);
file.close();
return bytes;
}
private static void writeToFile(String filePath, String data, int position)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
file.seek(position);
file.write(data.getBytes());
file.close();
}
}

myFile.TXT包含文本“此类用于读取和写入随机访问文件”。

运行程序后,它将包含

本课程用于阅读我爱我的国家和人民。