Java - RandomAccessFile read(byte[] b) method



Description

The Java RandomAccessFile read(byte[] b) method reads up to b.length bytes of data from this file into an array of bytes.

read(byte[] b) method −

  • Reads up to b.length bytes from the file into the given byte array b.

  • Returns the number of bytes actually read, or -1 if the end of file (EOF) is reached.

Declaration

Following is the declaration for java.io.RandomAccessFile.read(byte[] b) method.

public int read(byte[] b)

Parameters

b − The buffer into which the data is read.

Return Value

This method returns the total number of bytes read into the buffer, or -1 if there is no more data because the end of this file has been reached.

Exception

  • IOException> − If an I/O error occurs. Not thrown if end-of-file has been reached.

  • NullPointerException − If b is null.

Example - Usage of RandomAccessFile read(byte[] b) method

The following example shows the usage of RandomAccessFile read(byte[] b) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
   
      try {
         byte[] b1 = {1, 2, 3};
         byte[] b2 = {1, 2, 3, 4, 5, 6, 7, 8};

         // 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 8 bytes and print the number of bytes read
         System.out.println("" + raf.read(b1));

         // set the file pointer at 0 position
         raf.seek(0);

         // read the first 8 bytes and print the number of bytes read
         System.out.println("" + raf.read(b2));
      } 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 −

3
8

Example - Read all data from a file into a byte array

The following example shows the usage of RandomAccessFile read(byte[] b) 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_bytes1.txt", "rw")) {
         raf.write("HelloWorld".getBytes());

         byte[] buffer = new byte[10];
         raf.seek(0); // move pointer to start of file
         int bytesRead = raf.read(buffer);

         System.out.println("Bytes read: " + bytesRead);
         System.out.println("Content: " + new String(buffer));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Bytes read: 10
Content: HelloWorld

Explanation

  • Writes "HelloWorld" (10 bytes) to the file.

  • Uses a 10-byte buffer to read all bytes in one go.

  • Converts and prints the result as a string.

Example - Partial read when buffer is smaller than file content

The following example shows the usage of RandomAccessFile read(byte[] b) 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_bytes2.txt", "rw")) {
         raf.write("abcdefghij".getBytes()); // 10 bytes

         byte[] buffer = new byte[4];
         raf.seek(6); // Move to 7th byte (index 6)
         int bytesRead = raf.read(buffer);

         System.out.println("Bytes read: " + bytesRead);
         System.out.println("Content read from offset 6: " + new String(buffer, 0, bytesRead));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Bytes read: 4
Content read from offset 6: ghij

Explanation

  • Writes "abcdefghij" (10 bytes) to file.

  • Seeks to byte index 6, which is 'g'.

  • Reads 4 bytes (g, h, i, j) into a smaller buffer.

  • Shows how to read a part of a file using seek + buffer.

java_io_randomaccessfile.htm
Advertisements