Open In App

Java.io.RandomAccessFile Class Method | Set 2

Improve
Improve
Like Article
Like
Save
Share
Report

Set 1, Set 3
Methods of Java.io.RandomAccessFile Class Method :

  1. readLine() : java.io.RandomAccessFile.readLine() reads the next line of text from this file, start reading from the File Pointer till the end of file.
    Syntax : 
    public final String readLine()
    Parameters : 
    -----
    Return : 
    reads the next line of text from this file
    
  2. readUnsignedByte() : java.io.RandomAccessFile.readUnsignedByte() reads an unsigned 8 bit number from file, starts reading from the current File Pointer.
    Syntax : 
    public final int readUnsignedByte()
    Parameters : 
    ------
    Return : 
    reads an unsigned 8 bit number from file
    
  3. readUnsignedShort() : java.io.RandomAccessFile.readUnsignedShort() reads an unsigned 16 bit number from file, starts reading from the current File Pointer.
    Syntax : 
    public final int readUnsignedShort()
    Parameters : 
    -------
    Return : 
    reads an unsigned 16 bit number from file
    
  4. readUTF() : java.io.RandomAccessFile.readUTF() reads in a string from the file
    Syntax : 
    public final String readUTF()
    Parameters : 
    -------
    Return : 
    Unicode String
    
  5. seek(long pos) : java.io.RandomAccessFile.seek(long pos) sets File pointer position.
    Syntax : 
    public void seek(long pos)
    Parameters : 
    pos : start position from file, measured in bytes
    Return : 
    --------
    
  6. setLength(long len) : java.io.RandomAccessFile.setLength(long len) sets length of the file.
    Syntax : 
    public void setLength(long len)
    Parameters : 
    len : desired length of the file
    Return : 
    -------
    
  7. skipBytes(int n) :java.io.RandomAccessFile.skipBytes(int n) skip over n bytes, discarding the skipped bytes
    Syntax : 
    public int skipBytes(int n)
    Parameters : 
    n : no. of bytes to be skipped
    Return : 
    no. of bytes skipped
    
  8. getChannel() : java.io.RandomAccessFile.getChannel() returns unique FileChannel object associated with file.
    Syntax : 
    public final FileChannel getChannel()
    Parameters : 
    ------
    Return : 
    returns unique FileChannel object
    
  9. java.io.RandomAccessFile.length() : returns length of the file.
    Syntax : 
    public long length()
    Parameters : 
    ----
    Return : 
    length of the file, measured in bytes
    
  10. getFilePointer() : java.io.RandomAccessFile.getFilePointer() return current offset in the file in bytes.
    Syntax : 
    public long getFilePointer()
    Parameters : 
    ----
    Return : 
    return current offset in the file in bytes
    
  11. getFD() : java.io.RandomAccessFile.getFD() returns file descriptor object with the stream.
    Syntax : 
    public final FileDescriptor getFD()
    Parameters : 
    -----------
    Return : 
    file descriptor object with the stream.
    
  12. close() : java.io.RandomAccessFile.close() closes random access file stream and releases source associated with the stream, if any.
    Syntax : 
    public void close()
    Parameters : 
    -------
    Return : 
    -------
    



  13. // Java Program illustrating use of io.RandomAccessFile class methods
    // seek(), readLine(), readUTF(), readUnsignedByte(), readUnsignedShort(),
    // setLength(), length(), skipBytes(), getFilePointer(), getChannel(),
    // getFD(), close()
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            try
            {
                // Creating a new RandomAccessFile - "GEEK"
                RandomAccessFile geek = new RandomAccessFile("FILE.txt", "rw");
      
                // Writing to file
                geek.writeUTF("Hello Geeks For Geeks");
      
                geek.seek(0);
      
                // Use of readUTF() :
                System.out.println("Use of readUTF() : " + geek.readUTF());
      
                //Use of seek() :
                geek.seek(0);
      
                // Use of readLine() :
                System.out.println("1 readLine() : " + geek.readLine());
                geek.seek(0);
      
                geek.writeUTF("Hello \nGeeks For Geeks");
                geek.seek(0);
      
                System.out.println("2 readLine() : " + geek.readLine());
      
                geek.seek(3);
                // Use of readUnsignedByte() :
              System.out.println("Use of readUnsignedByte() :  " + geek.readUnsignedByte());
      
                geek.seek(4);
                // Use of readUnsignedShort() :
              System.out.println("Use of readUnsignedByte() :  " + geek.readUnsignedShort());
      
                // Use of setLength():
                geek.setLength(78);
      
                // Use of length() :
                System.out.println("Use of setLength() : " + geek.length());
      
                geek.seek(2);
                // Use of skipBytes() :
                System.out.println("Use of skipBytes() : " + geek.skipBytes(3));
      
      
                // Use of getFilePointer() :
                System.out.println("Use of getFilePointer() : " + geek.getFilePointer());
      
                // Use of getChannel() :
                System.out.println("Use of getChannel() : " + geek.getChannel());
      
                // Use of getFD() :
                System.out.println("Use of getFD() : " + geek.getFD());
      
                // Use of close() method :
                geek.close();
                System.out.println("Stream Closed.");
      
            }
            catch (IOException ex)
            {
                System.out.println("Something went Wrong");
                ex.printStackTrace();
            }
        }
    }

    
    

    Output :

    Use of readUTF() : Hello Geeks For Geeks
    1 readLine() : Hello Geeks For Geekss
    2 readLine() : Hello
    Use of readUnsignedByte() :  101
    Use of readUnsignedByte() :  27756
    Use of setLength() : 78
    Use of skipBytes() : 3
    Use of getFilePointer() : 5
    Use of getChannel() : sun.nio.ch.FileChannelImpl@15db9742
    Use of getFD() : java.io.FileDescriptor@6d06d69c
    Stream Closed.
    


    Last Updated : 25 Jan, 2023
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads