RandomAccessFile Class used to support reading and writing random access files. Provides a "file pointer", similar to cursors and subscripts, obtained using the getFilePointer() method, and setting the subscript using the seek() method.
public RandomAccessFile(String name, String mode){}
mode: the mode of file operation, this parameter has a fixed input string: "r", "rw", "rws", "rwd"
Read file content:
public int read(){};
Read one byte of file data. A byte is returned as an integer from 0-255, with -1 being returned to indicate that the end of the file has been reached.
public int read(byte b[], int off, int len){}
Read a certain length of bytes from the file and put them into the array of parameter 1. Return -1 to indicate that the end of the file has been reached.
public int read(byte b[]){}
Read bytes of length b.length from the file and put them into the parameter b.
public final String readLine(){}
Read the next line of the file
seek() method: Sets the offset of the file pointer. If the offset exceeds the file length, the file length will not be changed.
public long length(){} Get the length of the file
public void setLength(long newLength){} Set the length of the file. If it is smaller than the original, the extra files will be cut off.
use:
public static void readFile(String filename) throws Exception{
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
raf.writeInt(1);
raf.writeChars("A");
}