***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***

posts

    Java – RandomAccessFile

    The Java.io.RandomAccessFile class file is used for reading and writing to random access file. A random access file behaves like a large array of bytes. The RandomAccessFile class has several methods used to move the cursor position in a file to perform read or write operations.

    Constructors:

    1. RandomAccessFile(File file, String mode)

    Creates a random access file stream to read from, and optionally write to, the file specified by the File argument.

    2. RandomAccessFile(String name, String mode)

    Creates a random access file stream to read from, and optionally write to, a file with the specified name.

    The RandomAccessFile class in java has the following methods:

    S.No.

    Methods with Description

    1

    int read()

    It reads byte of data from a file. The byte is returned as an integer in the range 0-255.

    2

    int read(byte[] b)

    It reads byte of data from file upto b.length, -1 if end of file is reached.

    3

    int read(byte[] b, int offset, int len)

    It reads bytes initialising from offset position upto b.length from the buffer.

    4

    boolean readBoolean()

    It reads a boolean value from from the file.

    5

    byte readByte()

    It reads signed eight-bit value from file.

    6

    char readChar()

    It reads a character value from file.

    7

    double readDouble()

    It reads a double value from file.

    8

    float readFloat()

    It reads a float value from file.

    9

    long readLong()

    It reads a long value from file.

    10

    int readInt()

    It reads a integer value from file.

    11

    void readFully(byte[] b)

    It reads bytes initialising from offset position upto b.length from the buffer.

    12

    void readFully(byte[] b, int offset, int len)

    It reads bytes initialising from offset position upto b.length from the buffer.

    13

    String readUTF()

    t reads in a string from the file.

    14

    void seek(long pos)

    It sets the file-pointer(cursor) measured from the beginning of the file, at which the next read or write occurs.

    15

    long length()

    It returns the length of the file.

    16

    void write(int b)

    It writes the specified byte to the file from the current cursor position.

    17

    void writeFloat(float v)

    It converts the float argument to an int 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 writeDouble(double v)

    It converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first.

    Example:

    import java.io.IOException; 

    import java.io.RandomAccessFile; 

     public class RandomAccessFileExample { 

        static final String FILEPATH ="myFile.TXT"; 

        public static void main(String[] args) { 

            try { 

                System.out.println(new String(readFromFile(FILEPATH, 0, 18))); 

                writeToFile(FILEPATH, "this is program for Randomacess", 31); 

            } catch (IOException e) { 

                e.printStackTrace(); 

            } 

        } 

        private static byte[] readFromFile(String filePath, int position, int size) 

                throws IOException { 

            RandomAccessFile file = new RandomAccessFile(filePath, "r"); 

            file.seek(position); 

            byte[] bytes = new byte[size]; 

            file.read(bytes); 

            file.close(); 

            return bytes; 

        } 

        private static void writeToFile(String filePath, String data, int position) 

                throws IOException { 

            RandomAccessFile file = new RandomAccessFile(filePath, "rw"); 

            file.seek(position); 

            file.write(data.getBytes()); 

            file.close(); 

        } 

    } 

    No comments:

    Post a Comment