Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Java Programming » IO » Random access files in Java
Suryateja Pericherla Categories: IO. No Comments on Random access files in Java
0
(0)

A random-access file is a file in which the user or programmer can read and write data to the file at any location (hence the name random) simultaneously. A random-access file behaves like a large array of bytes. To navigate through the file, there is a file pointer which can be moved to the desired location.
 

The RandomAccessFile in Java is a pre-defined class that belongs to java.io package which allows the programmers to work with random access files. While moving the file pointer, if end of file is reached, it throws an EOFException. The constructors in RandomAccessFile class are given below:

Constructor Description
RandomAccessFile(File file, String mode) Creates a random-access file stream for reading and writing to the file based on the specified mode.
RandomAccessFile(String string, String mode) Creates a random-access file stream for reading and writing to the file based on the specified file name and mode.

 

Some of the methods available in the RandomAccessFile class are given below:

Method Description
void close() Closes the random-access file stream and releases any resources associated with it
int read Reads a byte of data from the file
int readInt() Reads a signed 32-bit integer from the file
String readUTF() Reads a string from the file
void seek(long pos) Sets the file pointer offset from the beginning of the file
void write(int b) Writes a specified byte to the file
long length() Returns the length of the file

 

Following Java program demonstrates reading and writing to a random-access file using the RandomAccessFile class:

import java.io.*;
public class RandomAccess
{
	public static void main(String[] args) throws Exception
	{
		RandomAccessFile raf = new RandomAccessFile("rand.txt", "rw");
		int size = (int)raf.length();
		System.out.println("File length = " + size + " bytes.");
		
		//Code for reading data from the file
		raf.seek(10);
		byte[] data = new byte[size];
		raf.read(data);
		System.out.println("Content in the file = " + new String(data));
		
		//Code for writing data to the file
		raf.seek(size);
		String s = "New piece of text.";
		raf.write(s.getBytes());
		raf.close();
	}
}

 

Output for the above program is:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


File length = 30 bytes.
Content in the file = sample line of text.

 

When you open rand.txt and see the content, it will be:

This is a sample line of text.New piece of text.

 

For more information, visit the following links:

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram