Instances of RandomAccessFile support both reading and writing to a random access file.
A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek method.
Benefits of Using Random Access File
Unlike IO streams that allow reading and writing data sequentially, random access file allows you to read and write a small chunk of data at any position in the file, using a file pointer. By controlling this file pointer, you can move forth and back within the file to perform reading and writing operations, at any position you want, in a random fashion.
Random access file is ideal for manipulating a particular file format at low level, such as reading, creating and updating MP3 files, PDF files, etc.
Constructors
Access Modes
Few Important Methods
Note: For reading and writing, as the RandomAccessFile class implements both the DataOutput and DataInput interfaces, you can use some of the below methods.
Reading methods: read(byte[]), readByte(), readInt(), readLong(), etc.
Writing methods: write(byte[]), writeByte(), writeInt(), writeLong(), etc.
Please visit RandomAccessFile Documentation for the entire list of methods.
package com.java.io;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileExample {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
String data = "Testdata1data2data3data4";
try {
randomAccessFile = new RandomAccessFile("/Test/TestFolder/test-file50.txt", "rw");
byte[] bytes = data.getBytes();
randomAccessFile.write(bytes);
//Setting the pointer position to 10.
randomAccessFile.seek(10);
System.out.println("Initial Pointer:" + randomAccessFile.getFilePointer());
//Reading the byte at current position.
int readData = randomAccessFile.read();
while (readData != -1) {
System.out.print("Read Character:" + (char) readData);
System.out.print(" , ");
// The pointer is moved by 1 after each read call.
System.out.print("New Pointer:" + randomAccessFile.getFilePointer());
System.out.println("");
readData = randomAccessFile.read();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Initial Pointer:10
Read Character:a , New Pointer:11
Read Character:t , New Pointer:12
Read Character:a , New Pointer:13
Read Character:2 , New Pointer:14
Read Character:d , New Pointer:15
Read Character:a , New Pointer:16
Read Character:t , New Pointer:17
Read Character:a , New Pointer:18
Read Character:3 , New Pointer:19
Read Character:d , New Pointer:20
Read Character:a , New Pointer:21
Read Character:t , New Pointer:22
Read Character:a , New Pointer:23
Read Character:4 , New Pointer:24
Read Character:a , New Pointer:25
Read Character:t , New Pointer:26
Read Character:a , New Pointer:27
Read Character:4 , New Pointer:28