Java IO Tutorial - Java RandomAccessFile .readFully (byte[] b, int off, int len)








Syntax

RandomAccessFile.readFully(byte[] b, int off, int len) has the following syntax.

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

Example

In the following code shows how to use RandomAccessFile.readFully(byte[] b, int off, int len) method.

/*from   ww  w. j a va2s  .  co  m*/
import java.io.*;

public class Main {

   public static void main(String[] args) {
      try {
         String s = "Hello world from java2s.com";
         
         RandomAccessFile raf = new RandomAccessFile("c:/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);
         raf.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }


   }
}