Table of contents
1.
Introduction
2.
Java - RandomAccessFile
3.
Class declaration
4.
Class constructors
5.
Class methods
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Oct 15, 2024

Random Access File class

Author Aditya Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! An overview of Java RandomAcessFile is provided in the following article. RandomAccessFile is a java class that allows you to read and write random access files. The java.io.RandomAccessFile package is defined by the RandomAccessFile class, which is a built-in class in Java. The random access file is similar to a file system's array of bytes. The RandomAccessFile class allows you to read and write to a random access file. We can perform the read or write operation by moving the pointer to a specific position.

Random Access File Class

Also See, Multithreading in java, and Duck Number in Java.

Java - RandomAccessFile

This class allows you to read and write to a 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. EOFException is issued if the end-of-file is reached before the desired number of bytes has been read. It's an IOException type.

This class inherits methods from the Java.io.Object class.

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 Code

Class 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 Code

2. 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 Code

3. 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 Code

4. 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 Code

5. 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 Code

6. 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 Code

7. 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 Code

8. 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 Code

9. 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 Code

10. 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 Code

11. 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 Code

12. 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 Code

13. 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 Code

14. 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 Code

15. 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 Code

16. 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 Code

17. 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 Code

18. 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 Code

19. 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 Code

20. 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 Code

21. ava.io.RandomAccessFile.length() : returns length of the file.

Syntax : 

public long length()
You can also try this code with Online Java Compiler
Run Code

22. 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 Code

23. 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 Code

24. 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 Code

25. 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 Code

26. 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 Code

27. 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 Code

28. 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 Code

29. 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 Code

30. 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 Code

31. 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 Code

32. 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 Code

33. 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 Code

34. 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 Code

35. 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 Code

36. 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 Code

37. 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

  1. 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.

     
  2. 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.

     
  3. 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.

     
  4. 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!

Live masterclass