Java

Java RandomAccessFile

We will look at the RandomAccessFile class in this article. It uses a cursor to move the pointer location across the file. The ability to access the file randomly gives it the name, RandomAccessFile.

Example 1: Appending Text at the End of a File

In this example, we will add the text at the end of the following file using the methods of the RandomAccessFile class.

First, we import the java.io. RandomAccessFile class. Next, we make a new object of the RandomAccessFile by calling the constructor of the RandomAccessFile(“file.txt”, “rw”). With the filename and mode, this function Object() builds a RandomAccessFile stream. In this case, the mode is “rw” which launches the file in the read-write mode.

The “seek()” method is used in the next line with the parameter file. The length() method sets the cursor/pointer to the specified position. In the previous case, it is the end of the file text. Then, the writeBytes() method is called with the string (“ Ipsum”) as its parameters. This method writes the string starting from the current file-pointer or the cursor. f.close()  method closes the random access file stream. Any system resources used by the stream are released.

After compiling the given code, we can see the “Successfully added data” in the terminal which indicates that the code has run successfully. Then, if we open the “file.txt” file in which we appended the data in our code, we can see that our data is successfully appended at the end of the file.

Example 2: Reading Text From a Text File

In this example, we will read the contents of a text file using the methods of the RandomAccessFile class. The file to be read in this example is as shown in the following:

In the given code, we first import the “java.io.RandomAccessFile” in order to use its methods and functions. Here, we create a RandomAccessFile stream with mode “r” which represents that it is in read-only mode. The current offset in bytes from the file’s start is provided by the “f.getFilePointer()” function. Then, we supply this value to the seek() function which sets the file pointer to the beginning of the file where the next read takes place. Following that, we generate a 5-byte array. When the read method is called with this array as its parameter (f.read(bytes)), the 5 bytes are read from the file and are added to the 5 byte array.

The f.close() method closes the RandomAccessFile stream. Any system resources used by the stream are released.

Finally, the return bytes return the array in which the text from the file is read and added. This can be seen in the following that only 5 characters from the file are displayed on the terminal. 

Example 3: Writing Text in a Text File

In this example, we will use the seek(), write(), and close() methods to write the data in a text file. The get bytes function is also used in this example to write.

In the given code, we first import the java.io. RandomAccessFile to use its methods and functions. Here, we create a RandomAccessFile stream with mode “rw” which represents that it is in read-write mode. The f.seek(5) method moves the file pointer position to the 5th index. Then, the  f.write(“Data”.getBytes()) writes the new bytes of “Data” and replaces the existing bytes if any are present at this location.

The f.close() method closes the RandomAccessFile stream. Any system resources used by the stream are released.

After the compilation is completed, the “Successfully added data” string is displayed on the terminal to indicate that the code has run successfully. The text file where we wrote the text using the RandomAccessFile is shown in the following:

Example 4: Appending, Adding Text in a File, and Reading the Text of the File

In this example, we will implement all the different approaches that we can adopt with the RandomAccessFile class object and its multiple functions for writing, appending, and reading the data from a text file.

We create three different functions in a single class for writing, appending the data, and reading the content of the text file. In the append data and write data function, we use the write() function using the object of the RandomAccessFile class. In the append data function, we add the data after the last byte of the file using the file.length() function as a parameter in the seek() function. In the write data function, we insert the data at the start of the file by passing an integer value in the seek() function. By doing this, the append data function is able to add the data to the end of the file. In the last function, we use the read() function to read the bytes of the file. In this function, we pass two parameters which acts as the range from which we want to read the data from.

As we can see in the terminal, the output only shows the first 5 bytes of the file as the range is set in the read() function. Now, we open the text file and verify that the data is inserted and appended at the correct position.

As we can see in the previous snippet, the data in the text file is correctly inserted after we executed our Java program.

Conclusion                              

We discussed the use of the RandomAccessFile class in Java in this article. The object of this class is used to call the multiple functions which are lucrative in extracting and inserting the data in a text file. We implemented several examples of the different methods of this class in the Ubuntu 20.04 environment. We used the write(), seek(), and read() methods in these examples to insert the data into a text file and read the content of the text file after that.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.