Random Access Files
Random Access Files
Instances of this class support both reading and writing to a random access file.
A random access file behaves like a large array of bytes stored in the file system.
A random-access file consists of a sequence of bytes. It supports a special pointer known
as file pointer. A file pointer indicates the current position (location) in the file.
It is positioned at one of these bytes in the file and can be moved to any arbitrary position
in the file prior to reading or writing.
In other words, a read or write operation takes place at the location of the file pointer. The
file pointer can be moved using seek() method provided by RandomAccessFile class.
When a file is first created, the file pointer is set to 0, indicating the beginning of the file.
When we read or write data to the file using read or write methods, the file pointer moves
forward to the next data item (i.e. next byte).
For example, if we read an int value using readInt() method from the file, JVM reads 4
bytes using file pointer, and now the file pointer is 4 bytes ahead of the previous position,
as shown in figure below.
For a RandomAccessFile raf, the raf.seek(position) method moves the file pointer to a
specified location. raf.seek(0) moves the file pointer to the beginning of the file, and
raf.seek(raf.length()) moves it to the end of the file.
2. long getFilePointer(): This method returns the current offset (in bytes) from the
beginning of the file to where the next read or write occurs.
3. long length(): This method returns the length (number of bytes) of this
file.
4. int read(): It reads a byte of data from this file and returns –1 at the
end of the stream.
5. int read(byte[ ] b): It reads up to b.length bytes of data from this file into an
array of bytes.
6. boolean readBoolean(): This method reads a boolean value from this file.
7. char readChar(): This method reads a character value from this file.
8. double readDouble(): The readDouble() method reads a double value from this
file.
9. float readFloat(): The readFloat() method reads a float value from this file.
10. String readUTF(): The readUTF() method reads in a string from this file.
11. void seek(long pos): This method sets the file-pointer (in bytes specified in
pos), measured from the beginning of this file, at which
the next read or write occurs.
12. void write(byte[ ] b): This method writes b.length bytes from the specified byte
array to this file, starting at the current file pointer.
13. void write(int b): The write() method writes the specified byte to this file.
14. void writeBoolean(boolean v): It writes a boolean value (one byte) to the file.
15. void writeChar(int v): It writes a char to the file as a two-byte value, high byte
first.
16. void writeDouble(double v): It first converts the specified double value to a long value
using the doubleToLongBits method in class Double, and
then writes that long value to the file as an eight-byte
quantity, high byte first.
17. void writeFloat(float v): It first converts the specified float value to an int value
using the floatToIntBits method in class Float and then
writes that int value to the file as a four-byte quantity, high
byte first.
18. void writeInt(int v): It writes an int value to the file as four bytes, high byte
first.
19. void writeLong(long v): It writes a long value to the file as eight bytes, high byte
first.
20. void writeUTF(String str): It writes a string value to the file using modified UTF-8
encoding in a machine-independent manner.
Ex:1
import java.io.*;
class F1
{
public static void main(String[] args)throws Exception
{
RandomAccessFile raf=new RandomAccessFile("raf1.txt", "rw");
String s ="Hello Programmers How are You";
raf.write(s.getBytes());
raf.seek(0);
System.out.println("Current pointer position:"+raf.getFilePointer());
//Current pointer position:0
int i=raf.read();
System.out.println("Character at 0 position:"+(char)i);
//Character at 0 position:H
i=raf.read();
System.out.println("Character at next position::"+(char)i);
//Character at next position::e
raf.seek(6);
System.out.println("Current pointer position:"+raf.getFilePointer());
//Current pointer position:6
i=raf.read();
System.out.println("Character at 6th position::"+(char)i);
//Character at 6th position::P
raf.close();
}
}
Ex:2
import java.io.*;
class F1
{
public static void main(String[] args)throws Exception
{
RandomAccessFile raf=new RandomAccessFile("raf1.txt", "rw");
String s ="Hello Programmers How are You";
byte [] b=s.getBytes();
raf.write(b);
file.writeChar('S');
file.writeInt(2222);
file.writeDouble(222.22);
file.seek(2);
System.out.println(file.readInt()); //2222
file.seek(file.length());
file.writeBoolean(true);
// Since pointer is at the end, beyond the 4th item, brings the file pointer to the 4th item in
the file.
file.seek(4);
System.out.println(file.readBoolean()); // Reading 4th item.
//true
file.close(); // Closing stream.
}
}
Ex:4
import java.io.IOException;
import java.io.RandomAccessFile;
class RandomIO1
{
public static void main(String[] args) throws IOException
{
// Create a random access file object.
RandomAccessFile file = new RandomAccessFile("raf1.txt", "rw");
// Retrieve the new number. Move the file pointer to new number.
file.seek(11 * 4);
System.out.println("New number: "+file.readInt());
// New number: 333
}
}
EX:5
import java.io.*;
class NewClass
{
public static void main(String[] args) throws Exception
{
// Creating a new RandomAccessFile - "raf.txt"
RandomAccessFile raf1= new RandomAccessFile("raf2.txt", "rw");
raf1.seek(0);
System.out.println("Use of readUtF() method : " + raf1.readUTF());
//Use of readUtF() method : The purpose of our life is to be happy
}
}