Class declaration
Following is the declaration for Java.io.RandomAccessFile class:-
public class RandomAccessFile
extends Object
implements DataOutput, DataInput, Closeable

You can also try this code with Online Java Compiler
Run CodeClass constructors

Class methods
1. read() : java.io.RandomAccessFile.read() reads byte of data from file. The byte is returned as an integer in the range 0-255
Syntax :
public int read()

You can also try this code with Online Java Compiler
Run Code2. read(byte[] b) java.io.RandomAccessFile.read(byte[] b) reads bytes upto b.length from the buffer.
Syntax :
public int read(byte[] b)

You can also try this code with Online Java Compiler
Run Code3. read((byte[] b, int offset, int len) : java.io.RandomAccessFile.read((byte[] b, int offset, int len) reads bytes initialising from offset position upto b.length from the buffer.
Syntax :
public int read(byte[] b, int offset, int len)

You can also try this code with Online Java Compiler
Run Code4. readBoolean() : java.io.RandomAccessFile.readBoolean() reads a boolean from the file.
Syntax :
public final boolean readBoolean()

You can also try this code with Online Java Compiler
Run Code5. readByte() : java.io.RandomAccessFile.readByte() reads a signed eight-bit value from file, start reading from the File Pointer.
Syntax :
public final byte readByte()

You can also try this code with Online Java Compiler
Run Code6. readChar() : java.io.RandomAccessFile.readChar() reads a character from the file, starts reading from the File Pointer.
Syntax :
public final char readChar()

You can also try this code with Online Java Compiler
Run Code7. readDouble(): java.io.RandomAccessFile.readDouble() reads a double value from the file and starts reading from the File Pointer.
Syntax :
public final double readDouble()

You can also try this code with Online Java Compiler
Run Code8. readFloat(): java.io.RandomAccessFile.readFloat() reads a float value from the file and starts reading from the File Pointer.
Syntax :
public final double readFloat()

You can also try this code with Online Java Compiler
Run Code9. readFully(byte[] b) : java.io.RandomAccessFile.readFully(byte[] b) reads bytes upto b.length from the buffer, start reading from the File Pointer.
Syntax :
public final void readFully(byte[] b)

You can also try this code with Online Java Compiler
Run Code10. readInt(): java.io.RandomAccessFile.readInt() reads a signed 4 bytes integer from the file and starts reading from the File Pointer.
Syntax:
public final void readInt();

You can also try this code with Online Java Compiler
Run Code11. readFully(byte[] b, int offset, int len): java.io.RandomAccessFile.readFully(byte[] b, int offset, int len) reads bytes initialising from offset position up to b.length from the buffer, start reading from the File Pointer.
Syntax :
public final void readFully(byte[] b, int offset, int len)

You can also try this code with Online Java Compiler
Run Code12. readLong(): java.io.RandomAccessFile.readLong() reads a signed 64-bit integer from the file and starts reading from the File Pointer.
Syntax :
public final long readLong()

You can also try this code with Online Java Compiler
Run Code13. readLine(): java.io.RandomAccessFile.readLine() reads the next line of text from this file, start reading from the File Pointer till the end of the file.
Syntax :
public final String readLine()

You can also try this code with Online Java Compiler
Run Code14. 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()

You can also try this code with Online Java Compiler
Run Code15. 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()

You can also try this code with Online Java Compiler
Run Code16. readUTF() : java.io.RandomAccessFile.readUTF() reads in a string from the file
Syntax :
public final String readUTF()

You can also try this code with Online Java Compiler
Run Code17. seek(long pos) : java.io.RandomAccessFile.seek(long pos) sets File pointer position.
Syntax :
public void seek(long pos)

You can also try this code with Online Java Compiler
Run Code18. setLength(long len) : java.io.RandomAccessFile.setLength(long len) stes length of the file.
Syntax :
public void setLength(long len)

You can also try this code with Online Java Compiler
Run Code19. skipBytes(int n): java.io.RandomAccessFile.skipBytes(int n) skip over n bytes, discarding the skipped bytes
Syntax :
public int skipBytes(int n)

You can also try this code with Online Java Compiler
Run Code20. getChannel() : java.io.RandomAccessFile.getChannel() returns unique FileChannel object associated with file.
Syntax :
public final FileChannel getChannel()

You can also try this code with Online Java Compiler
Run Code21. ava.io.RandomAccessFile.length() : returns length of the file.
Syntax :
public long length()

You can also try this code with Online Java Compiler
Run Code22. getFilePointer() : java.io.RandomAccessFile.getFilePointer() return current offset in the file in bytes.
Syntax :
public long getFilePointer()

You can also try this code with Online Java Compiler
Run Code23. getFD() : java.io.RandomAccessFile.getFD() returns file descriptor object with the stream.
Syntax :
public final FileDescriptor getFD()

You can also try this code with Online Java Compiler
Run Code24. close(): java.io.RandomAccessFile.close() closes the random access file stream and releases the source associated with the stream if any.
Syntax :
public void close()

You can also try this code with Online Java Compiler
Run Code25. write(int bytes): java.io.RandomAccessFile.write(int bytes) writes a specified byte to the file, starting from the current file pointer.
Syntax :
public void write(int bytes)

You can also try this code with Online Java Compiler
Run Code26. write(byte[] b): java.io.RandomAccessFile.write(byte[] b) starts from the current file pointer and writes b.length bytes from the supplied byte array to the file.
Syntax :
public void write(byte[] b)

You can also try this code with Online Java Compiler
Run Code27. 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)

You can also try this code with Online Java Compiler
Run Code28. writeBoolean(boolean b) : java.io.RandomAccessFile.writeBoolean(boolean b)
writes a boolean to file as a one-byte value. True is written if the value is ‘1’ else, false
Syntax :
public final void writeBoolean(boolean b)

You can also try this code with Online Java Compiler
Run Code29. writeByte(int b) : java.io.RandomAccessFile.writeByte(int b)
writes a byte to the file as a one-byte value, starting from the current position.
Syntax :
public final void writeByte(int b)

You can also try this code with Online Java Compiler
Run Code30. writeShort(int b): java.io.RandomAccessFile.writeShort(int b) writes a short to a file as a two-byte value, starting from the current position.
Syntax :
public final void writeShort(int b)

You can also try this code with Online Java Compiler
Run Code31. writeChar(int c): java.io.RandomAccessFile.writeChar(int c) writes a char to the file as a two-byte value, starting from the current position.
Syntax :
public final void writeChar(int c)

You can also try this code with Online Java Compiler
Run Code32. writeInt(int i): java.io.RandomAccessFile.writeInt(int i) writes an int to a file as a four-byte value, starting from the current position.
Syntax :
public final void writeInt(int i)

You can also try this code with Online Java Compiler
Run Code33. writeLong(long l): java.io.RandomAccessFile.writeLong(long l) writes an int to a file as an eight-byte value, starting from the current position.
Syntax :
public final void writeLong(long l)

You can also try this code with Online Java Compiler
Run Code34. writeFloat(float f): java.io.RandomAccessFile.writeFloat(float f) transforms the float input to an int, then writes an int as a four-byte value to the file, starting at the current position.
Syntax :
public final void writeFloat(float f)

You can also try this code with Online Java Compiler
Run Code35. writeDouble(double d) : java.io.RandomAccessFile.writeDouble(double d)
converts the double input to a long, then writes an int as an 8-byte value to the file, starting at the current place.
Syntax :
public final void writeDouble(double d)

You can also try this code with Online Java Compiler
Run Code36. writeBytes(String s): java.io.RandomAccessFile.writeBytes(String s) writes a string to a file as a sequence of bytes.
Syntax :
public final void writeBytes(String s)

You can also try this code with Online Java Compiler
Run Code37. writeUTF(String str) : java.io.RandomAccessFile.writeUTF(String str) uses modified UTF-8 encoding to save a string to a file (machine-independent)
Syntax :
public final void writeUTF(String str)

You can also try this code with Online Java Compiler
Run Code
With the help of these syntax you can practice it on online java compiler.
Frequently Asked Questions
- Which classes support both reading and writing to a random access file?
Closeable, DataInput, DataOutput, AutoCloseable Instances of this class support reading and writing to a random access file.
- What is file handling in Java?
In simple words, file handling means reading and writing data to a file. In Java, the concept Stream is used in order to perform I/O operations on a file.
- What is a random access file (RAF)?
When we receive data from or write data to a random access file, the file pointer goes ahead. The file pointer is a cursor that indicates where we will begin our next read or write.
- What is eofexception in random access file?
A random access file works in the same way as a huge array of bytes. The array has a cursor called a file pointer, and we move the cursor to perform read and write operations. when the end-of-file is reached before the desired number of bytes has been read EOFException is issued. It is a type of IOException.
Conclusion
In this article, we have extensively discussed Random Access File Class in java. We have learned how to declare random access file class. We have also learned different types of constructors and class methods.
We hope that this blog has helped you enhance your knowledge regarding Random Access File Class and if you would like to learn more, check out our articles on File Handling in Java. Do upvote our blog to help other ninjas grow.
Read about Interface in Java here
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Reading!