- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - RandomAccessFile readByte() method
Description
The Java RandomAccessFile readByte() method reads a signed eight-bit value from this file. This method reads a byte from the file, starting from the current file pointer.
Declaration
Following is the declaration for java.io.RandomAccessFile.readByte() method.
public final byte readByte()
Parameters
NA
Return Value
This method returns the next byte of this file as a signed eight-bit byte.
Exception
IOException − If an I/O error occurs. Not thrown if end-of-file has been reached.
EOFException − If this file has reached the end.
Example - Usage of RandomAccessFile readByte() method
The following example shows the usage of RandomAccessFile readByte() method.
RandomAccessFileDemo.java
package com.tutorialspoint;
import java.io.RandomAccessFile;
import java.io.IOException;
public class RandomAccessFileDemo {
public static void main(String[] args) {
try {
// create a new RandomAccessFile with filename test
RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
// write something in the file
raf.writeUTF("Hello World");
// set the file pointer at 0 position
raf.seek(0);
// read byte
System.out.println(raf.readByte());
// set the file pointer at 0 position
raf.seek(0);
// write 0 at the start
raf.write(0);
// read byte
System.out.println(raf.readByte());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output
Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −
ABCDE
Let us compile and run the above program, this will produce the following result −
0 11
Example - Reading bytes from a file one by one
The following example shows the usage of RandomAccessFile readByte() method.
RandomAccessFileDemo.java
package com.tutorialspoint;
import java.io.RandomAccessFile;
import java.io.IOException;
public class RandomAccessFileDemo {
public static void main(String[] args) {
try {
RandomAccessFile raf = new RandomAccessFile("sample.dat", "rw");
// Write some bytes for demonstration
raf.writeByte(65); // 'A'
raf.writeByte(66); // 'B'
raf.writeByte(67); // 'C'
// Reset file pointer to the start
raf.seek(0);
// Read and print each byte
System.out.println("Byte 1: " + (char) raf.readByte()); // A
System.out.println("Byte 2: " + (char) raf.readByte()); // B
System.out.println("Byte 3: " + (char) raf.readByte()); // C
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Byte 1: A Byte 2: B Byte 3: C
Explanation
writeByte() writes raw bytes into the file.
readByte() reads one byte at a time and returns it as a signed byte (-128 to 127).
We cast it to char for readable output (prints A, B, C).
This is useful when dealing with low-level binary file formats.
Example - Reading a signed byte and checking its value
The following example shows the usage of RandomAccessFile readByte() method.
RandomAccessFileDemo.java
package com.tutorialspoint;
import java.io.RandomAccessFile;
import java.io.IOException;
public class RandomAccessFileDemo {
public static void main(String[] args) {
try {
RandomAccessFile raf = new RandomAccessFile("numbers.dat", "rw");
// Write byte values (some negative)
raf.writeByte(127); // Max positive byte
raf.writeByte(-128); // Min negative byte
raf.writeByte(-1); // -1 in byte is 0xFF
// Reset pointer
raf.seek(0);
byte b1 = raf.readByte();
byte b2 = raf.readByte();
byte b3 = raf.readByte();
System.out.println("First byte: " + b1); // 127
System.out.println("Second byte: " + b2); // -128
System.out.println("Third byte: " + b3); // -1
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
First byte: 127 Second byte: -128 Third byte: -1
Explanation
readByte() returns a signed byte, which can be negative.
This example shows how byte values over 127 (like 0xFF, 0x80) are interpreted as negative numbers.
Useful in networking, bytecode, or any protocol that uses signed byte data.