Java - RandomAccessFile readFully(byte[] b,int off,int len) method



Description

The Java RandomAccessFile readFully(byte[] b,int off,int len) method reads exactly len 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,int off,int len) method.

public final void readFully(byte[] b,int off,int len)

Parameters

  • b − the buffer into which the data is read.

  • off − the start offset of the data.

  • len − the number of bytes to 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,int off,int len) method

The following example shows the usage of RandomAccessFile readFully(byte[] b,int off,int len) 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[10];

         // read the file
         raf.readFully(arr, 3, 7);

         // 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

Example - Read specific bytes into part of an array

The following example shows the usage of RandomAccessFile readFully(byte[] b,int off,int len) 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("example1.dat", "rw");

         // Write 6 bytes to the file
         raf.write(new byte[]{1, 2, 3, 4, 5, 6});

         // Go to the second byte (value 2)
         raf.seek(1);

         // Create a byte array of size 5
         byte[] buffer = new byte[5];

         // Read 3 bytes starting at position 1 in the array
         raf.readFully(buffer, 1, 3);

         // Print the buffer
         for (byte b : buffer) {
            System.out.print(b + " ");
         }
         // Output: 0 2 3 4 0

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

Output

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

0 2 3 4 0

Explanation

  • readFully(buffer, 1, 3) reads 3 bytes from the file into buffer[1], buffer[2], and buffer[3].

  • The rest of the array remains as default (0).

  • This is useful when you want to fill only part of an array from a file.

Example - Read a segment of data from a specific file position

The following example shows the usage of RandomAccessFile readFully(byte[] b,int off,int len) 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("example2.dat", "rw");

         // Write 10 bytes to the file
         for (int i = 10; i < 20; i++) {
            raf.write(i); // writes 10 to 19
         }

         // Seek to byte position 5 (value 15)
         raf.seek(5);

         // Prepare array
         byte[] data = new byte[6];

         // Read 4 bytes into data[2] to data[5]
         raf.readFully(data, 2, 4);

         // Print array contents
         for (byte b : data) {
            System.out.print(b + " ");
         }
         // Output: 0 0 15 16 17 18

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

Output

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

0 0 15 16 17 18

Explanation

  • raf.seek(5) moves the file pointer to byte 5 (value 15).

  • readFully(data, 2, 4) reads 4 bytes into the array starting at index 2.

  • Bytes 15, 16, 17, 18 are read into data[2] to data[5].

java_io_randomaccessfile.htm
Advertisements