Random Access Files

I’m not used to technical blogging and just want to clarify this blog is mainly to help my understanding of topics and to share what I’ve learnt so that it may help others 🙂

So I said I’d do a post ever week and I think what’s holding me back from blogging more is:

  • finding/making the time
  • wanting to create really good quality posts

So I would rather put a lot of thought and value into a post than just write for the sake of it. One thing I’ve noticed is that a lot of people LOVE to make things more complicatded than they actually are. So with this in mind, I’ve made a decision to wherever possible try and keep things, including code examples as simple as possible. Today I have decided to write about Random Access Files in Java as this is what I’m currently focusing on right now.

In order to be able to use random access files in Java we need to first import the random access file package: import java.io.RandomAccessFile This enables us to use the Random Access File class and its associated methods.

The code below basically opens a file, reads the file length (one of the inbuilt methods) and outputs its result. An important thing to note is when using the length() method we must also catch an IOException otherwise the program will not compile.


import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;

public class hello{

public static void main(String[] args){
    RandomAccessFile raf = new RandomAccessFile(new File("hello.txt"),"rw");
    System.out.println("file opened / read succesfully");
    raf.writeUTF("hello world!");

    catch(IOException e){
        System.out.println("error opening / reading file");

As can be seen, when creating the file ‘hello.txt’ we need to specify how we want to use this file, by the second argument, the first is the file name, which is a string. In this example, I have said that we want to be able to read and write, which is signified by the initals ‘rw’. If I only wanted to read I would, as you probably can guess, put only ‘r’ as the second argument.

One of the ways in which we can gain random access to the file is by using the seek() method. The seek method basically lets the user access a specific place in the file, which is determined by the argument passed to the seek() function. The RAF is treated as a binary file and must be handled as a stream of bytes. The file pointer is set to the argument given in the seek() method, which is the position in bytes.

When a file is first opened, the pointer is always set to 0. It’s really important to be familiar with datatypes and there associated memory if we want to navigate succesfully through Random Access Files, for example if an integer is being read, the pointer will be moved by 4 bytes, because this is the size of an integer.

A brief overview of datatypes and their size is shown below:


Again, there are more complex tables available but for this post I just want to keep things simple to enhance clarity and understanding. Another really useful method is .writeUTF() and the way I am using here, it will add the text ‘hello world’ into the file hello.txt. It’s useful to note that when I ran the program without writing the file the System.out.println(raf.length()) command printed 0 but after including the writeUTF(“hello world!”) the value printed is now 14 because this is within the file, as can be seen in the output below.


~/workspace/Java/ $ java hello
file opened / read succesfully

I hope this post was helpful and informative and if you have any questions please get in touch by using the ‘contact’ link above 🙂 Please remember if you want to run this code locally then you will need to create a ‘hello.txt’ file in the same directory for this to work.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s