Open In App

Java.io.RandomAccessFile Class Method | Set 3

Last Updated : 13 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Set 1, Set 2

More Methods of io.RandomAccessFile Class :

  1. write(int bytes) : java.io.RandomAccessFile.write(int bytes) writes specified byte to the file, starting from the current file pointer.
    Syntax : 
    public void write(int bytes)
    Parameters :
    bytes : bytes to be written 
    Return :
    -----
    Exception :
    IOException :in case an I/O error occurs.
    
  2. write(byte[] b) : java.io.RandomAccessFile.write(byte[] b) writes b.length bytes from the specific byte array to the file, starting from the current file pointer.
    Syntax : 
    public void write(byte[] b)
    Parameters :
    b : data to be written
    Return :
    -------
    Exception :
    IOException :in case an I/O error occurs.
    
  3. write(byte[] b, int offset, int len) : java.io.RandomAccessFile.write(byte[] b, int offset, int len) writes bytes initialising from offset position upto b.length from the buffer.
    Syntax : 
    public int write(byte[] b, int offset, int len)
    Parameters :
    b : buffer to write
    offset : starting position to write
    len : max no of bytes to write
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  4. writeBoolean(boolean b) : java.io.RandomAccessFile.writeBoolean(boolean b)
    writes a boolean to file as one-byte value. True is written is value is ‘1’ else false

    Syntax : 
    public final void writeBoolean(boolean b)
    Parameters :
    b : boolean value to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  5. writeByte(int b) : java.io.RandomAccessFile.writeByte(int b)
    writes a byte to file as one-byte value, starting from the current position.
    Syntax : 
    public final void writeByte(int b)
    Parameters :
    b : byte to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  6. writeShort(int b) : java.io.RandomAccessFile.writeShort(int b) writes a short to file as two-byte value, starting from the current position.
    Syntax : 
    public final void writeShort(int b)
    Parameters :
    b : short to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  7. writeChar(int c) : java.io.RandomAccessFile.writeChar(int c) writes a char to file as two-byte value, starting from the current position.
    Syntax : 
    public final void writeChar(int c)
    Parameters :
    c : char to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  8. writeInt(int i) : java.io.RandomAccessFile.writeInt(int i) writes a int to file as four-byte value, starting from the current position.
    Syntax : 
    public final void writeInt(int i)
    Parameters :
    i : int value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  9. writeLong(long l) : java.io.RandomAccessFile.writeLong(long l) writes a int to file as eight-byte value, starting from the current position.
    Syntax : 
    public final void writeLong(long l)
    Parameters :
    l : long value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  10. writeFloat(float f) : java.io.RandomAccessFile.writeFloat(float f) converts the float argument to int and then writes a int to file as four-byte value, starting from the current position.
    Syntax : 
    public final void writeFloat(float f)
    Parameters :
    f : float value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  11. writeDouble(double d) : java.io.RandomAccessFile.writeDouble(double d)
    converts the double argument to long and then writes a int to file as 8-byte value, starting from the current position.

    Syntax : 
    public final void writeDouble(double d)
    Parameters :
    d : double value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  12. writeBytes(String s) : java.io.RandomAccessFile.writeBytes(String s) writes string to file as a sequence of bytes.
    Syntax : 
    public final void writeBytes(String s)
    Parameters :
    s : byte string to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  13. writeUTF(String str) : java.io.RandomAccessFile.writeUTF(String str) writes a string to file using modified UTF-8 encoding(machine-independent)
    Syntax : 
    public final void writeUTF(String str)
    Parameters :
    str : String to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    



  14. // Java Program illustrating use of io.RandomAccessFile class methods
    // writeUTF(), writeChar(), writeDouble(), writeFloat(), write(byte[] b), 
    // write(int i), writeBoolean(), writeLong(), writeShort()
    // write(byte[] b, int offset, int len)
       
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            try
            {
                long l = 458754545576l;
                double d = 1.5;
                float f = 14.56f;
                int i = 1;
                boolean bol = true;
                short s = 15
       
                // Creating a new RandomAccessFile - "GEEK"
                RandomAccessFile geek = new RandomAccessFile("GEEK.txt", "rw");
       
                // writeUTF() : 
                geek.writeUTF("Hello Geeks For Geeks");
                  
                geek.seek(0);
                System.out.println("writeUTF : " + geek.readUTF()); 
                geek.seek(0);
       
                byte[] b = {1, 2, 3, 6, 5, 4};
                  
                // write(byte[] b) : 
                geek.write(b);
                  
                geek.seek(0);
                System.out.println("Use of .read(byte[] b) : " + geek.read(b));
                  
                // write(int i) :  
                geek.write(i);
                
                geek.seek(0);
                System.out.println("write(int i) : " + geek.read(b));
                  
                // writeBoolean() : 
                geek.writeBoolean(bol);            
                  
                geek.seek(0);
                System.out.println("writeBoolean() : " + geek.readBoolean());
       
                geek.write(b, 2, 2);
                geek.seek(0);
          System.out.println("write(byte[] b, int offset, int len) : " + geek.readByte());
       
                // writeChar() : 
                geek.writeChar('c');
                  
                geek.seek(0);
                System.out.println("writeChar() : " + geek.readChar()); 
                geek.seek(0);
                  
                // writeDouble() : 
                geek.writeDouble(d);
                  
                geek.seek(0);            
                System.out.println("writeDouble() : " + geek.readDouble()); 
                geek.seek(0);
                  
                //writeFloat() : 
                geek.writeFloat(f);
                  
                geek.seek(0);
                System.out.println("writeFloat() : " + geek.readFloat()); 
                  
                // writeLong() :
                geek.writeLong(l);
                  
                geek.seek(0);
                System.out.println("writeLong() : " + geek.readLong()); 
                  
                // writeShort() :
                geek.writeShort(s);
                  
                geek.seek(0);
                System.out.println("writeShort() : " + geek.readShort());  
                         
            }
            catch (IOException ex)
            {
                System.out.println("Something went Wrong");
                ex.printStackTrace();
            }
        }
    }

    
    

    Output :

    writeUTF : Hello Geeks For Geeks
    Use of .read(byte[] b) : 6
    write(int i) : 6
    writeBoolean() : true
    write(byte[] b, int offset, int len) : 1
    writeChar() : c
    writeDouble() : 1.5
    writeFloat() : 14.56
    writeLong() : 4713287227910652010
    writeShort() : 16744
    


    Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

Similar Reads