Java RandomAccessFile specifies the location for file reading and writing

  • 2020-05-27 05:26:19
  • OfStack

Java RandomAccessFile specifies the location for file reading and writing

As a random read class, RandomAccessFile can operate directly on the content of the file itself randomly. It can read and write the content in the specified location of the file, which is very convenient in many cases.

RandomAccessFile is used to access files that hold data records, so you can use the seek() method to access the records and read and write. These records need not be the same size; But its size and location must be known. But this class is limited to action files.

RandomAccessFile does not belong to the family InputStream and OutputStream. In fact, apart from implementing the DataInput and DataOutput interfaces (which DataInputStream and DataOutputStream also implement), it has nothing to do with the two classes, or even with any of the functionality that already exists in the InputStream and OutputStream classes; It is a completely independent class, and all the methods (most of which belong to itself) are written from scratch. This is probably because RandomAccessFile can move back and forth within a file, so its behavior is fundamentally different from other I/O classes. All in all, it's a separate class that directly inherits Object.

Basically, RandomAccessFile works by combining DataInputStream and DataOutputStream, plus some of its own methods, such as getFilePointer() for locating, seek() for moving around files, and length() and skipBytes() for determining file sizes. In addition, its constructor also has a parameter to indicate whether the file is opened read-only ("r") or read-write ("rw") (as well as fopen()1 of C). It does not support writing only files.

Only RandomAccessFile has the seek search method, and this method only applies to files. BufferedInputStream has an mark() method that you can use to set the tag (save the result in an internal variable) and then call reset() to return the location, but it's too weak and impractical.

Example code:


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

/**
 * RandomAccessFile It belongs to the random read class, which can operate directly on the content of the file itself randomly, that is, it can specify the location 
 *  The read and write contents of 
 * @author andy
 *
 */
public class RandomAccessFileTest {

 public static void main(String args[]) throws IOException {
 write();
 read();
 }
 
 public static void write() throws IOException {
 // Access the file by reading and writing 
 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);// Sets the position of the pointer 
 boolean booleanValue = raf.readBoolean();
 int intValue = raf.readInt();
 double doubleValue = raf.readDouble();
 raf.seek(0);// Sets 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();// Every time reading 1 And assign it to the byte bytes[i]
 String stringValue = new String(bytes);
 raf.skipBytes(1);// A pointer to skip 1 bytes 
 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 to help you, thank you for your support of this site!


Related articles: