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



Description

The Java RandomAccessFile write(byte[] b,int off,int len) method writes len bytes from the specified byte array starting at offset off to this file.

Declaration

Following is the declaration for java.io.RandomAccessFile.write(byte[] b,int off,int len) method.

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

Parameters

  • b − The byte to be written.

  • off − The start offset in the data.

  • len − The number of bytes to write.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile write(byte[] b,int off,int len) method

The following example shows the usage of RandomAccessFile write(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 {
         byte[] b = {1, 2, 3, 4, 5, 6};
         
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("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());
         
      } 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
4

Example - Write a Portion of a Byte Array to a File

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

         // Full byte array
         byte[] data = "HELLO_WORLD".getBytes(); // 11 bytes

         // Write only "WORLD" (start at index 6, length 5)
         raf.write(data, 6, 5);

         // Reset pointer and read result
         raf.seek(0);
         byte[] readBack = new byte[5];
         raf.readFully(readBack);

         System.out.println("Written data: " + new String(readBack)); // Output: WORLD
         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Written data: WORLD

Explanation

  • write(data, 6, 5) starts at index 6 ('W') and writes 5 bytes ("WORLD").

  • This method is ideal when you need to write a substring or part of a byte array.

Example - Overwrite Part of a File Using Array Segment

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

         // Write initial string
         raf.writeBytes("1234567890");

         // New data to insert: overwrite "456" with "XYZ"
         byte[] update = "XYZabc".getBytes(); // full array
         raf.seek(3); // move to 4th byte (position of '4')
         raf.write(update, 0, 3); // only write "XYZ"

         // Read full content to confirm overwrite
         raf.seek(0);
         byte[] result = new byte[10];
         raf.readFully(result);

         System.out.println("Updated file: " + new String(result)); // Output: 123XYZ7890

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

Output

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

Updated file: 123XYZ7890

Explanation

  • write(update, 0, 3) writes only the first 3 bytes of the array ("XYZ"), ignoring the rest.

  • File content is partially overwritten without altering length or unrelated data.

  • This is useful for precise file ing.

java_io_randomaccessfile.htm