Random Access Files in java

The random access files as name suggest, you can access the content of file from anywhere. The access here means read or write. That means you don't have to depend upon writing in a blank file or appending at the end of file only.

The Random Access Files are the files in which you don't remain depending upon the content access at the beggening of the file or appending at the end of the file. There is a file pointer which can be positioned anywhere and you can do the file operation at that position of the file. The Java core IO package, java.io provides the RandomAccessFile class which supports this functionality.

Using this class is not different from using other byte or character class for the file IO. This is simple operation with only one additional step, that step is optional, but necessary in case you want to do some random access functionality. The process is of pointer management. You don't need to worry, this is not the 'C' or 'C++' pointer we are talking about. This is the file position pointer. This is the pointer which tells the current access location of the file to the program. When you open the file initially this is at position 0.

You can manage the pointer location using the method provided with the RandomAccessFile class, seek(long position); now this has a return type void and it advances the pointer to the position we pass as an argument. this is a long type argument because the file could be large in size.

The following program will enlighten the use of RandomAccessFile class, for the demonstration purpose we are using a file with name inputfile.txt in which we have added following four lines.

line one in input file.
line two in input file.
line three in input file.
last line in input file.

So now let us look at the program.

import java.io.*;

class RA{
    public static void main(String[] args) throws IOException{
        RandomAccessFile ras = null;
            ras = new RandomAccessFile("inputfile.txt","r");
        catch(Exception ioe){

In the program first of all we have created an object of RandomAccessFile which takes as parameter the file path(these is another constructor which takes the File object as parameter) and the operation type for the file. Here we are reading from file so using only "r", but if you want to open the file for read-write operation you should use the "rw" operation type.

Next we print out the position of the file pointer using the method, getFilePointer(), this will return the current location of the file pointer. So we read the line, and after reading we are moving the pointer to the position 20 using the seek() method. again we have repeated the the operation. Here we have used the readLine() method to read the whole line, but there are other method in RandomAccessFile like readInt(), readDouble(), readChar() etc. You can use whichever suits your requirement.

The output of the program is as follows:

line one in input file.

As we told you the initial position of the pointer is 0, it prints 0 and read the line at position. After that we move the pointer to position 20 we read again, and it will print the le. as the position 20 is the second line "line two in input fi*le." marked by the *.

Note: please remember that what we pass in the seek() method is actually the offset which is added to the current position on the file pointer, its not from the initial location of the pointer, that is 0.

So this way we read the line and again check the position of pointer which is now 24. You see, this way we can access the file randomly to the position where we want.

The possible use of this class and operation is to do file operation to any randomly selected part of file or to mark any bookmark on any file.