RandomAccessFile 

Introduction

The Java.io.RandomAccessFile class file behaves like a large array of bytes stored in the file system.Instances of this class support both reading and writing to a random access file.

Class declaration

Following is the declaration for Java.io.RandomAccessFile class −

public class RandomAccessFile
   extends Object
      implements DataOutput, DataInput, Closeable

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

Class constructors

Sr.No.Constructor & Description
1RandomAccessFile(File file, String mode)This creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
2RandomAccessFile(File file, String mode)This creates a random access file stream to read from, and optionally to write to, a file with the specified name.

Class methods

Sr.No.Method & Description
1void close()This method Closes this random access file stream and releases any system resources associated with the stream.
2FileChannel getChannel()This method returns the unique FileChannel object associated with this file.
3FileDescriptor getFD()This method returns the opaque file descriptor object associated with this stream.
4long getFilePointer()This method returns the current offset in this file.
5long length()This method returns the length of this file.
6int read()This method reads a byte of data from this file.
7int read(byte[] b)This method reads up to b.length bytes of data from this file into an array of bytes.
8int read(byte[] b, int off, int len)This method reads up to len bytes of data from this file into an array of bytes.
9boolean readBoolean()This method reads a boolean from this file.
10byte readByte()This method reads a signed eight-bit value from this file.
11char readChar()This method reads a character from this file.
12double readDouble()This method reads a double from this file.
13float readFloat()This method reads a float from this file.
14void readFully(byte[] b)This method reads b.length bytes from this file into the byte array, starting at the current file pointer.
15void readFully(byte[] b, int off, int len)This method reads exactly len bytes from this file into the byte array, starting at the current file pointer.
16int readInt()This method reads a signed 32-bit integer from this file.
17String readLine()This method reads the next line of text from this file.
18long readLong()This method reads a signed 64-bit integer from this file.
19short readShort()This method reads a signed 16-bit number from this file.
20int readUnsignedByte()This method reads an unsigned eight-bit number from this file.
21int readUnsignedShort()This method reads an unsigned 16-bit number from this file.
22String readUTF()This method reads in a string from this file.
23void seek(long pos)This method sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
24void setLength(long newLength)This method sets the length of this file.
25int skipBytes(int n)This method attempts to skip over n bytes of input discarding the skipped bytes.
26void write(byte[] b)This method writes b.length bytes from the specified byte array to this file, starting at the current file pointer.
27void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this file.
28void write(int b)This method writes the specified byte to this file.
29void writeBoolean(boolean v)This method writes a boolean to the file as a one-byte value.
30void writeByte(int v)This method writes a byte to the file as a one-byte value.
31void writeBytes(String s)This method writes the string to the file as a sequence of bytes.
32void writeChar(int v)This method writes a char to the file as a two-byte value, high byte first.
33void writeChars(String s)This method writes a string to the file as a sequence of characters.
34void writeDouble(double v)This method 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.
35void writeFloat(float v)This method 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.
36void writeInt(int v)This method writes an int to the file as four bytes, high byte first.
37void writeLong(long v)This method writes a long to the file as eight bytes, high byte first.
38void writeShort(int v)This method writes a short to the file as two bytes, high byte first.
39void writeUTF(String str)This method writes a string to the file using modified UTF-8 encoding in a machine-independent manner.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Object

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *