Java - ObjectOutputStream writeChar(int val) method



Description

The Java ObjectOutputStream writeChar(int val) method method writes a 16 bit char.

  • writeChar(int v) writes a 2-byte Unicode character (16 bits) to the output stream.

  • It writes the high byte first, then the low byte - big-endian format.

  • It only writes the character's code (not the whole Character object).

  • It's inherited from DataOutput, which ObjectOutputStream implements.

  • This is not object serialization - it's raw character data writing.

Declaration

Following is the declaration for java.io.ObjectOutputStream.writeChar(int val) method.

public void writeChar(int val)

Parameters

  • val − The char to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur while writing to the underlying stream.

Example - Usage of ObjectOutputStream writeChar(int val) method

The following example shows the usage of ObjectOutputStream writeChar(int val) method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      char s = 'H';
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeChar(s);
         oout.writeChar('E');

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print what we wrote before
         System.out.println("" + ois.readChar());
         System.out.println("" + ois.readChar());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

H 
E

Example - Write a single character to a file and read it back

The following example shows the usage of ObjectOutputStream writeChar(int val) method. We're writing the character 'A' using writeChar() and read it back.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "char1.bin";

      // Write a single char
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeChar('A');  // Unicode value 65
         System.out.println("Wrote character: A");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read the char
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         char ch = ois.readChar();
         System.out.println("Read character: " + ch);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote character: A
Read character: A

Example - Write multiple characters (like a word) using writeChar()

The following example shows the usage of ObjectOutputStream writeChar(int val) method. We're writing each letter of the word "Hi!" as individual characters.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "char2.bin";
      char[] letters = { 'H', 'i', '!' };

      // Write each character using writeChar
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         for (char ch : letters) {
            oos.writeChar(ch);
         }
         System.out.println("Wrote characters: H, i, !");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read and print each character
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         for (int i = 0; i < letters.length; i++) {
            char ch = ois.readChar();
            System.out.println("Read char: " + ch);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote characters: H, i, !
Read char: H
Read char: i
Read char: !
java_io_objectoutputstream.htm
Advertisements