
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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: !