Menu
Topics Index
...
`


Input/Output: Exploring java.io > Byte Streams >
Siva Nookala - 20 Feb 2017
Instances of RandomAccessFile class support both reading and writing to a file. There is a kind of cursor, or index called the file pointer. All read operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written.

The file pointer can be read by the getFilePointer method and set by the seek method.
It is generally true of all the reading routines in this class that if end-of-file is reached before the desired number of bytes has been read, an EOFException (which is a kind of IOException) is thrown. If any byte cannot be read for any reason other than end-of-file, an IOException other than EOFException is thrown. In particular, an IOException may be thrown if the stream has been closed.
Random Access File Demo
import java.io.*;

class RandomAccessFileDemo
{
    public static void main(String arg[])
    {
        try
        {
            String input = "Learn Java With Merit Campus";
            System.out.println("Input : " + input);
            RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
            raf.writeBytes(input);
        
            // MOVE TO 6TH POSITION AND READ FOUR CHARACTERS
            raf.seek(6);
            String s = "" + (char) raf.read() + (char) raf.read() + (char) raf.read() + (char) raf.read();
            System.out.println("String read is : " + s);
        
            // CHANGE 'With' TO 'From'
            raf.seek(11);
            raf.writeBytes("From");
            raf.seek(0);
            System.out.println("After changing : " + raf.readLine());
        
            // TRUNCATE TO 10 CHARACTERS
            raf.setLength(10);
            raf.seek(0);
            System.out.println("After truncating : " + raf.readLine());
        
            raf.close();
        }
        catch(IOException ioex)
        {
            System.out.println("Read write failed - " + ioex.getMessage());
        }    
    }
}
OUTPUT

Input : Learn Java With Merit Campus
String read is : Java
After changing : Learn Java From Merit Campus
After truncating : Learn Java

DESCRIPTION

We created a RandomAccessFile and have written the content Learn Java With Merit Campus into that file. Firstly we moved to the letter 'J' by using seek(6) and then read the next four characters, which are nothing but "Java". Later we moved to the letter 'W' using seek(11) and have written the bytes from the string "From". Finally we set the length to 10, which caused the file to be truncated to 10 characters and so it became just "Learn Java".

Constructor Description
RandomAccessFile(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
RandomAccessFile(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name.


Method Description
void close() Closes this random access file stream and releases any system resources associated with the stream.
long getFilePointer() Returns the current offset in this file.
long length() Returns the length of this file.
int read() Reads a byte of data from this file.
String readLine() Reads the next line of text from this file.
void seek(long pos) Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
void setLength(long newLength) Sets the length of this file.
void write(byte[] b) Writes b.length bytes from the specified byte array to this file, starting at the current file pointer.
void write(int b) Writes the specified byte to this file.
void writeBytes(String s) Writes the string to the file as a sequence of bytes.
void writeChars(String s) Writes a string to the file as a sequence of characters.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.