Java - RandomAccessFile write(byte[] b) method



Description

The Java RandomAccessFile write(byte[] b) method writes the specified byte to this file. The write starts at the current file pointer.

Declaration

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

public void write(byte[] b)

Parameters

b − The byte to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

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

The following example shows the usage of RandomAccessFile write(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 {
         int b1 = 15;
         int b2 = 20;
         
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write an int
         raf.write(b1);

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

         // print the byte
         System.out.println(raf.readByte());

         // write an int
         raf.write(b2);

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

         // print the byte
         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 −

15
20

Example - Writing a Byte Array to a File

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

         // Create a byte array with values
         byte[] data = {65, 66, 67, 68}; // A, B, C, D

         // Write entire byte array to file
         raf.write(data);

         // Move pointer to start and read back to verify
         raf.seek(0);
         byte[] readBack = new byte[4];
         raf.readFully(readBack);

         System.out.println("Written data: " + new String(readBack)); // Output: ABCD

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

Output

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

Written data: ABCD

Explanation

  • write(byte[] b) writes the entire byte array to the file.

  • In this example, it writes "ABCD".

  • Useful for saving blocks of raw binary or text data in one call.

Example - Overwriting Data Using write(byte[] b)

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

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

         // Move to position 3 (4th byte) and overwrite with "XYZ"
         raf.seek(3);
         byte[] overwrite = "XYZ".getBytes();
         raf.write(overwrite);

         // Read full content to verify changes
         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(byte[] b) overwrites data starting from the current file pointer.

  • seek(3) moves to the 4th byte before writing.

  • Useful for editing files in-place without rewriting the entire file.

java_io_randomaccessfile.htm
Advertisements