Java - RandomAccessFile write(int b) method



Description

The Java RandomAccessFile write(int 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(int b) method.

public void write(int 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(int b) method

The following example shows the usage of RandomAccessFile write(int 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 Single Bytes One at a Time

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

         // Write ASCII values for A, B, C
         raf.write(65); // A
         raf.write(66); // B
         raf.write(67); // C

         // Reset pointer and read back
         raf.seek(0);
         for (int i = 0; i < 3; i++) {
            int b = raf.read();
            System.out.print((char) b); // Output: ABC
         }

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

Output

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

ABC

Explanation

  • write(int b) writes the lowest 8 bits (1 byte) of the integer.

  • The values 65, 66, and 67 are ASCII codes for 'A', 'B', and 'C'.

  • This method is ideal for writing individual bytes, especially when building files byte-by-byte.

Example - Overwriting a Byte in the Middle of a File

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

         // Write initial data: "12345"
         raf.writeBytes("12345");

         // Overwrite the 3rd byte ('3') with 'X'
         raf.seek(2); // Byte position index starts at 0
         raf.write((int) 'X'); // ASCII 88

         // Read full file to verify
         raf.seek(0);
         byte[] content = new byte[5];
         raf.readFully(content);
         System.out.println("Updated content: " + new String(content)); // Output: 12X45

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

Output

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

Updated content: 12X45

Explanation

  • seek(2) positions the pointer at the 3rd byte.

  • write((int) 'X') replaces '3' with 'X'.

  • This shows how write(int b) can be used for in-place byte updates.

java_io_randomaccessfile.htm