Java - RandomAccessFile writeChar(int v) method



Description

The Java RandomAccessFile writeChar(int v) method writes a char to the file as a two-byte value, high byte first. The write starts at the current position of the file pointer.

Declaration

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

public final void writeChar(int v)

Parameters

v − a char value to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile writeChar(int v) method

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

         // write a char in the file
         raf.writeChar(i);

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

         // read char
         System.out.println(raf.readChar());

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

         // write a char at the start
         raf.writeChar(71);

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

         // read char
         System.out.println(raf.readChar());
         
      } 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 −

F 
G 

Example - Writing a Single Character to a File

The following example shows the usage of RandomAccessFile writeChar(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("output1.dat", "rw")) {
         raf.writeChar('A'); // Writing the character 'A'
         System.out.println("Character written successfully.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Character written successfully.

Explanation

  • 'A' has Unicode value 65 (0x0041).

  • writeChar('A') writes two bytes: 0x00 0x41.

  • The file output1.dat will now contain 2 bytes representing the character 'A'.

Example - Writing Multiple Characters and Reading Them Back

The following example shows the usage of RandomAccessFile writeChar(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("output2.dat", "rw")) {
         // Write characters
         raf.writeChar('X');
         raf.writeChar('Y');
         raf.writeChar('Z');

         // Move file pointer to the beginning
         raf.seek(0);

         // Read characters back
         char c1 = raf.readChar();
         char c2 = raf.readChar();
         char c3 = raf.readChar();

         System.out.println("Characters read: " + c1 + ", " + c2 + ", " + c3);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Characters read: X, Y, Z

Explanation

  • Three characters are written: 'X', 'Y', and 'Z'.

  • Each writeChar() writes 2 bytes → total 6 bytes.

  • seek(0) resets the pointer to the beginning.

  • readChar() reads back the 2-byte character in the same order.

java_io_randomaccessfile.htm