Java - RandomAccessFile writeByte(int v) method



Description

The Java RandomAccessFile writeByte(int v) method writes a byte to the file as a one-byte value. The write starts at the current position of the file pointer.

Declaration

Following is the declaration for java.io.RandomAccessFile.writeByte(int v) method.

public final void writeByte(int v)

Parameters

v − 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 writeByte(int v) method

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

         // write something in the file
         raf.writeByte(b1[0]);

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

         // read byte
         System.out.println(raf.readByte());

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

         // write 0 at the start
         raf.writeByte(b1[1]);

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

         // read 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
8

Example - Writing a Single Byte to a File

The following example shows the usage of RandomAccessFile writeByte(int v) 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("example1.dat", "rw");
         raf.writeByte(65); // ASCII value of 'A'
         raf.close();
         System.out.println("Byte written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Byte written to file.

Explanation

  • This code opens (or creates) a file named example1.dat in read-write (rw) mode.

  • It writes the byte 65, which is the ASCII code for 'A'.

  • writeByte(int b) writes only the lowest 8 bits of the provided integer. So even though 65 is an int, only the byte value is used.

  • After writing, the file contains one byte: 'A'.

Example - Overwriting a Byte at a Specific Position

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

         // Write three bytes: A, B, C
         raf.writeByte(65); // 'A'
         raf.writeByte(66); // 'B'
         raf.writeByte(67); // 'C'

         // Move the file pointer to the second byte (position 1)
         raf.seek(1);

         // Overwrite the second byte with 'Z' (ASCII 90)
         raf.writeByte(90);

         raf.close();
         System.out.println("Byte overwritten in file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Byte overwritten in file.

Explanation

  • First, this writes three bytes: 'A', 'B', and 'C' to example2.dat.

  • Then seek(1) moves the file pointer to the second byte (0-based index).

  • writeByte(90) overwrites 'B' with 'Z'.

  • Final file content is: 'A', 'Z', 'C'.

java_io_randomaccessfile.htm