Home »
Java programming language
Java RandomAccessFile readByte() Method with Example
RandomAccessFile Class readByte() method: Here, we are going to learn about the readByte() method of RandomAccessFile Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020
RandomAccessFile Class readByte() method
- readByte() method is available in java.io package.
- readByte() method is used to read byte value from this file as a signed eight-bit value.
- readByte() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
-
readByte() method may throw an exception at the time of reading byte.
- IOException: This exception may throw an exception while performing input/output operation.
- EOFException: This exception may throw when the file pointer reaches EOF (End-Of-File).
Syntax:
public final byte readByte();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is byte, it returns byte value of this RandomAccessFile as a signed 8 bits.
Example:
// Java program to demonstrate the example
// of byte readByte() method of
// RandomAccessFile
import java.io.*;
class RAFReadByte {
public static void main(String[] args) throws Exception {
// Instantiate a random access file
// object with file name and permissions
RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");
// By using writeUTF() method is to
// write data in the file
ra_f.writeUTF("Welcome, in Java World!!!");
// Initially set the file pointer
// is at 2 for reading the file
ra_f.seek(2);
// By using readByte() method isto
// read a byte value (signed 1 byte i.e.8 bit)
// from this file
byte by = ra_f.readByte();
System.out.println("ra_f.readByte(): " + by);
// By using close() method isto
// close this stream ran_f
ra_f.close();
}
}
Output
ra_f.readByte(): 87