Java - RandomAccessFile readFully(byte[] b) method



Description

The Java RandomAccessFile readFully(byte[] b) method reads b.length bytes from this file into the byte array, starting at the current file pointer. This method reads repeatedly from the file until the requested number of bytes are read.

Declaration

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

public final void readFully(byte[] b)

Parameters

b − the buffer into which the data is read.

Return Value

This method does not return a value.

Exception

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

  • EOFException − If this file reaches the end before reading all the bytes.

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

The following example shows the usage of RandomAccessFile readFully(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 {
         // create a string and a byte array
         String s = "Hello world";

         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write something in the file
         raf.writeUTF(s);

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

         // create an array equal to the length of raf
         byte[] arr = new byte[(int) raf.length()];

         // read the file
         raf.readFully(arr);

         // create a new string based on arr
         String s2 = new String(arr);

         // print it
         System.out.println("" + s2);
      } 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 −

Hello world

Example - Read an Entire Byte Array

The following example shows the usage of RandomAccessFile readFully(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("data.dat", "rw");

         // Write 5 bytes
         raf.write(new byte[]{10, 20, 30, 40, 50});

         // Reset pointer to beginning
         raf.seek(0);

         // Read all 5 bytes into an array
         byte[] buffer = new byte[5];
         raf.readFully(buffer);

         // Print the contents
         for (byte b : buffer) {
            System.out.print(b + " ");
         }
         // Output: 10 20 30 40 50

         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

10 20 30 40 50

Explanation

  • readFully(byte[] b) reads exactly b.length bytes.

  • If the file doesn't have enough bytes, it throws an EOFException.

  • It ensures complete reading — unlike read(), which may return fewer bytes.

Example - Read a Subset of Bytes into an Array

The following example shows the usage of RandomAccessFile readFully(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("partial.dat", "rw");

         // Write 6 bytes
         raf.write(new byte[]{100, 101, 102, 103, 104, 105});

         // Reset pointer to byte 2 (value 102)
         raf.seek(2);

         // Prepare buffer and read 3 bytes into offset 1
         byte[] buffer = new byte[5]; // default initialized with zeros
         raf.readFully(buffer, 1, 3); // fills buffer[1] to buffer[3]

         // Print the buffer content
         for (byte b : buffer) {
            System.out.print(b + " ");
         }
         // Output: 0 102 103 104 0

         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

0 102 103 104 0

Explanation

  • readFully(byte[] b, int off, int len) reads len bytes into the array starting at off.

  • In this case, it fills buffer[1], buffer[2], and buffer[3] with bytes from the file.

  • It does not return the number of bytes − it throws an exception if fewer bytes are available.

java_io_randomaccessfile.htm
Advertisements