Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java RandomAccessFile in action

farenda 2016-02-22 0

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!
Categories Java Tags java, java-io
Previous: Java read binary file
Next: Java File Permissions

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored