Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - ObjectInputStream readChar() method



Description

The Java ObjectInputStream readChar() reads a single 2-byte Unicode character (char) from the input stream. It is used when a character has been written using writeChar(char c).

Declaration

Following is the declaration for java.io.ObjectInputStream.readChar() method −

public char readChar()

Parameters

NA

Return Value

This method returns the 16 bit char read.

Exception

  • EOFException − If end of file is reached.

  • IOException − If an I/O error has occurred.

Example - Usage of ObjectInputStream readChar() method

The following example shows the usage of Java ObjectInputStream readChar() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      char b = 'F';
      
      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(b);
         oout.writeChar('A');
         oout.flush();

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

         // read and print a char
         System.out.println("" + ois.readChar());

         // read and print a char
         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−

F 
A

Example - Writing and Reading a Single Character

The following example shows the usage of Java ObjectInputStream readChar() method. This example writes a single character to a file and reads it back using readChar().

ObjectInputStreamDemo.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 ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing a single character to a file
         FileOutputStream fos = new FileOutputStream("char_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeChar('A'); // Writing character 'A'
         oos.close();

         // Reading character using ObjectInputStream
         FileInputStream fis = new FileInputStream("char_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         char value = ois.readChar(); // Read a single character
         System.out.println("Read Character: " + value);

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

Output

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

Read Character: A

Explanation

  • Writes a single character ('A') using writeChar('A').

  • Reads the character using readChar().

  • Prints the character, confirming it was correctly stored and retrieved.

Example - Writing and Reading Multiple Characters

The following example shows the usage of Java ObjectInputStream readChar() method. This example writes multiple characters ('H', 'e', 'l', 'l', 'o') and reads them sequentially.

ObjectInputStreamDemo.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 ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing multiple characters
         FileOutputStream fos = new FileOutputStream("multiple_chars.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeChar('H');
         oos.writeChar('e');
         oos.writeChar('l');
         oos.writeChar('l');
         oos.writeChar('o');
         oos.close();

         // Reading characters using ObjectInputStream
         FileInputStream fis = new FileInputStream("multiple_chars.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         System.out.print("Read Characters: ");
         for (int i = 0; i < 5; i++) {
            System.out.print(ois.readChar()); // Read and print each character
         }

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

Output

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

Read Characters: Hello

Explanation

  • Writes characters 'H', 'e', 'l', 'l', 'o' using writeChar(char c).

  • Reads them sequentially using readChar().

  • Prints "Hello", confirming correct deserialization.

java_io_objectinputstream.htm
Advertisements