Course Content‎ > ‎Session 6‎ > ‎

Random Access File

Using FileInputStream, you can read from a file and using FileOutputStream, you can write to a file. But if you want to read from and write to a file simultaneously, you will have to use the class RandomAccessFile. Instances of this class support reading and writing to a random access file. An application, which uses random access file, allows you to modify the position in the file at which the next read or write occurs. Any of the two constructors can be used:

    RandomAccessFile(File file, String mode);
or
    RandomAccessFile(String  name, String  mode);

The mode argument must either be equal to "r" or to "rw," indicating either to open the file for input, or for both input and output, respectively. For example, to open a file Demo.txt for both reading and writing, you will write:

    RandomAccessFile rf = new RandomAccessFile("Demo.txt","rw");

The methods associated with both DataInputStream and DataOutputStream are also associated with the RandomAccessFile class. In addition to the methods described in the table for DataInputStream and DataOutputStream, some of the methods are uniquely associated with the RandomAccessFile class. These methods are given below:

Method                   Description                                                                                                     
long length()            Returns the length of the file
fileDescriptor getFD()   Returns the file descriptor object associated with
                         this stream
boolean readBoolean()    Reads a boolean from file. This method reads a single
                         byte from the file. A value of 0 represents false. Any
                         other value represents true.
void seek(long pos)      Sets the offset from the beginning of this file at 
                         which the next read or write occurs.

Given below is an example in which the the contents typed through keyboard are input to the file random.dat, until the '*' character is typed. Then the contents of this same file i.e. random.dat are read:

    import java.io.*;
    public class fileOut1
    {
        public static void main(String args[]) throws IOException
        {
            int i=0;
            DataInputStream din = new DataInputStream(System.in);
            RandomAccessFile rf = new RandomAccessFile("random.dat","rw");
            
            char ch = ' ';
            while(ch != '*')
            {
                // until the character * is typed through the keyboard
                ch = (char)din.read();
                
                // read the character typed assign it to the variable ch
                rf.write(ch); // write the contents of the variable ch to random.dat
            }
            ch = ' ';
            
            // seek the beginning of file random.dat
            rf.seek(0);  
            // Until the end of file random.dat is reached
            while(i < rf.length()) 
            {
                // read the file random.dat
                ch = (char)rf.read();    
                
                
                // display the contents of file   
                System.out.println(ch);  
                i++;
            }
        }
    }

Comments