Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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: Mar 27, 2024

Random Access File class

Author Aditya Anand
1 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

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.

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Class declaration

Following is the declaration for Java.io.RandomAccessFile class:-

public class RandomAccessFile
   extends Object
      implements DataOutput, DataInput, Closeable

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()

2. read(byte[] b) java.io.RandomAccessFile.read(byte[] b) reads bytes upto b.length from the buffer.

          Syntax :

public int read(byte[] b)

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)

4. readBoolean() : java.io.RandomAccessFile.readBoolean() reads a boolean from the file.

Syntax :

public final boolean readBoolean()

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()

6. readChar() : java.io.RandomAccessFile.readChar() reads a character from the file, starts reading from the File Pointer.

Syntax :

public final char readChar()

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()

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()

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)

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();

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)

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()

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()

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()

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()

16. readUTF() : java.io.RandomAccessFile.readUTF() reads in a string from the file

Syntax : 

public final String readUTF()

17. seek(long pos) : java.io.RandomAccessFile.seek(long pos) sets File pointer position.

Syntax : 

public void seek(long pos)

18. setLength(long len) : java.io.RandomAccessFile.setLength(long len) stes length of the file.

Syntax : 

public void setLength(long len)

19. skipBytes(int n): java.io.RandomAccessFile.skipBytes(int n) skip over n bytes, discarding the skipped bytes

Syntax : 

public int skipBytes(int n)

20. getChannel() : java.io.RandomAccessFile.getChannel() returns unique FileChannel object associated with file.

Syntax : 

public final FileChannel getChannel()

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

Syntax : 

public long length()

22. getFilePointer() : java.io.RandomAccessFile.getFilePointer() return current offset in the file in bytes.

Syntax : 

public long getFilePointer()

23. getFD() : java.io.RandomAccessFile.getFD() returns file descriptor object with the stream.

Syntax : 

public final FileDescriptor getFD()

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()

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)


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