Java RandomAccessFile.write(byte[] b, int off, int len)

Syntax

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

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

Example

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


/* www. ja  va2 s .  c  om*/

import java.io.*;

public class Main {

   public static void main(String[] args) {
      try {
         byte[] b = {1, 2, 3, 4, 5, 6};
         
         RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

         // write 2 bytes in the file
         raf.write(b, 2, 2);

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

         // print the two bytes we wrote
         System.out.println(raf.readByte());
         System.out.println(raf.readByte());
         raf.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }

   }
}