Java RandomAccessFile specifies the location to implement file reading and writing
RandomAccessFile belongs to the random read class. It can operate the content of the file itself randomly and can read and write content at the specified location of the file, which is very convenient in many cases.
RandomAccessFile is used to access files that hold data records. You can use the seek ( ) method to access records and read and write. The size of these records does not have to be the same; however, their size and location must be known. However, this class is limited to operating files.
RandomAccessFile does not belong to the InputStream and OutputStream classes. In fact, in addition to implementing the DataInput and DataOutput interfaces (DataInputStream and DataOutputStream also implement these two interfaces), it has nothing to do with these two classes, and does not even use any functions that already exist in the InputStream and OutputStream classes; it is a completely independent class, and all methods (mostly belong to itself) are written from scratch. This may be because RandomAccessFile can move back and forth in the file, so its behavior is somewhat fundamentally different from other I/O classes. In short, it is a separate class that directly inherits Object.
Basically, the working way of RandomAccessFile is to combine DataInputStream and DataOutputStream, and add some of its own methods, such as getFilePointer( ) for positioning, seek( ) for moving in the file, and determine the length( ) for file size, and skipBytes() for skip. In addition, its constructor also has a parameter that indicates whether to open the file in read-only mode ("r") or read-write mode ("rw") (exactly the same as the fopen( ) of C). It does not support writing only files.
Only RandomAccessFile has a seek search method, and this method is only applicable to files. BufferedInputStream has a mark( ) method, which you can use to set the mark (save the result in an internal variable), and then call reset( ) to return to this position, but its function is too weak and not very practical.
Example code:
import java.io.IOException;import java.io.RandomAccessFile;/** * RandomAccessFile belongs to a random read class and can operate the content of the file itself directly and randomly, that is, you can specify the location * to read and write content* @author andy * */public class RandomAccessFileTest { public static void main(String args[]) throws IOException { write(); read(); } public static void write() throws IOException { //Access the file in a read-write manner RandomAccessFile raf = new RandomAccessFile("D://test.txt", "rw"); raf.writeBytes("Hello World!"); raf.writeBoolean(true); raf.writeInt(30); raf.writeDouble(3.56); raf.close(); } public static void read() throws IOException { RandomAccessFile raf = new RandomAccessFile("D://test.txt", "r"); raf.seek(12);//Set the pointer's position boolean booleanValue = raf.readBoolean(); int intValue = raf.readInt(); double doubleValue = raf.readDouble(); raf.seek(0);//Set the position of the pointer to the beginning of the file byte[] bytes = new byte[12]; for (int i=0; i<bytes.length; i++) bytes[i] = raf.readByte();//Read a byte at a time and assign it to bytes[i] String stringValue = new String(bytes); raf.skipBytes(1);//Skip a byte byte int intValue2 = raf.readInt(); raf.close(); System.out.println(booleanValue); System.out.println(intValue); System.out.println(doubleValue); System.out.println(stringValue); System.out.println(intValue2); } }Thank you for reading, I hope it can help you. Thank you for your support for this site!