Java Programming Tutorials

Java programming tutorials with many code examples!

Java RandomAccessFile in action

Problem:

How to use Java RandomAccessFile to read, write, and update files? In this post we’re going to show how to use it to perform the common operations.

Unlike other types in java.io package, RandomAccessFile allows to traverse a file back and forth, read and update data. To do that it provides own methods to write primitive Java types in specified number of bytes – for example an integer is written using 4 bytes.

RandomAccessFile takes access permission as a constructor parameter. Only the following values are valid, else it will throw an exception:

  • r
    Open a file for only reading.
  • rw
    Open for reading and writing.
  • rws
    Like “rw“, but also require that every update to the file’s content or metadata be written synchronously to the underlying storage device.
  • rwd
    Like “rw“, but also require that every update to the file’s content be written synchronously to the underlying storage device.

Solution:

In the following example we’re going to use java.io.RandomAccessFile to create a file with defined structure (records), display it, update selected record, and display the contests again:

package com.farenda.java.io;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

public class RandomAccessFileExample {

    // 4 chars on 2 bytes each:
    private static final int USERNAME_SIZE = 4 * 2;
    private static final int SCORE_SIZE = 4;
    private static final int RECORD_SIZE
        = USERNAME_SIZE + SCORE_SIZE;

    private final String filename;

    public RandomAccessFileExample(String filename) {
        this.filename = filename;
    }

    private void changeRecord(int record, String name, int points)
            throws IOException {
        try (RandomAccessFile file = new RandomAccessFile(filename, "rw")) {
            // "-1" because the first record is at byte 0:
            file.seek(RECORD_SIZE * (record-1));
            file.writeChars(name);
            file.writeInt(points);
        }
    }

    private void printContent() throws IOException {
        try (RandomAccessFile file = new RandomAccessFile(filename, "r")) {
            long nrecords = file.length() / RECORD_SIZE;
            System.out.printf(
                    "Total number of records in \"%s\": %d%n",
                    filename, nrecords);

            while (nrecords-- > 0) {
                System.out.printf("%s: %d%n",
                        readUser(file), file.readInt());
            }
        }
    }

    private String readUser(RandomAccessFile file) throws IOException {
        return new String(new char[] {
                file.readChar(), file.readChar(),
                file.readChar(), file.readChar()
        });
    }

    private void writeRecords(Map<String, Integer> scores)
            throws IOException {
        try (RandomAccessFile file = new RandomAccessFile(filename, "rw")) {
            for (Map.Entry<String,Integer> userScore : scores.entrySet()) {
                file.writeChars(userScore.getKey());
                file.writeInt(userScore.getValue());
            }
        }
    }

    public static void main(String[] args) throws IOException {
        Map<String, Integer> highScores = generateHighScores();
        RandomAccessFileExample app = new RandomAccessFileExample("user.data");
        app.writeRecords(highScores);
        app.printContent();
        app.changeRecord(3, "Mike", 9999);
        app.printContent();
    }

    private static Map<String, Integer> generateHighScores() {
        Random random = new Random();
        Map<String,Integer> highScores = new LinkedHashMap<>(5);
        for (int i = 1; i <= 5; ++i) {
            highScores.put("usr"+i, random.nextInt(1000));
        }
        return highScores;
    }
}

As expected, when you run the above program, you’ll see the following result:

Total number of records in "user.data": 5
usr1: 206
usr2: 748
usr3: 443
usr4: 647
usr5: 199
Total number of records in "user.data": 5
usr1: 206
usr2: 748
Mike: 9999
usr4: 647
usr5: 199

Note that the 3rd record has been updated!

RandomAccessFile is a separate class that allows to jump around the file and it doesn’t implement InputStream nor OutputStream interfaces, so cannot be combined with other stream processing classes from java.io.

Share with the World!