
- 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 read() method
Description
The Java RandomAccessFile read() method reads a byte of data from this file. The byte is returned as an integer in the range 0 to 255 (0x00−0x0ff).
read() method −
Reads a single byte of data from the file.
Returns −
The byte read, as an int (0-255)
-1 if end of file (EOF) is reached.
Useful for reading raw bytes one at a time.
Declaration
Following is the declaration for java.io.RandomAccessFile.read() method.
public int read()
Parameters
NA
Return Value
This method returns the next byte of data, or -1 if the end of the file has been reached.
Exception
IOException − If an I/O error occurs. Not thrown if end-of-file has been reached.
Example - Usage of RandomAccessFile read() method
The following example shows the usage of RandomAccessFile read() method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.RandomAccessFile; 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 the first byte and print it System.out.println("" + raf.read()); // set the file pointer at 4rth position raf.seek(4); // read the first byte and print it System.out.println("" + raf.read()); } 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 108
Example - Read a file byte-by-byte
The following example shows the usage of RandomAccessFile read() 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("read_example1.txt", "rw")) { raf.writeBytes("ABC"); // Write some bytes raf.seek(0); // Move to beginning for reading int byteData; while ((byteData = raf.read()) != -1) { System.out.println("Read byte: " + byteData + " (char: " + (char) byteData + ")"); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Read byte: 65 (char: A) Read byte: 66 (char: B) Read byte: 67 (char: C)
Explanation
Writes ABC to a file (each character = 1 byte).
Uses read() to read each byte until EOF.
Prints both the integer value and character.
Example - Reading a specific byte using seek() and read()
The following example shows the usage of RandomAccessFile read() 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("read_example2.txt", "rw")) { raf.writeBytes("1234567890"); raf.seek(5); // Move file pointer to byte index 5 int data = raf.read(); System.out.println("Byte at position 5: " + data + " (char: " + (char) data + ")"); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Byte at position 5: 54 (char: 6)
Explanation
Writes digits 1−0 into the file.
Seeks to byte index 5 (i.e., 6th character).
Reads and prints the character at that position ('6').