Java - ObjectOutputStream writeChars(String val) method



Description

The Java ObjectOutputStream writeChars(String val) method writes a String as a sequence of chars.

  • Writes each character of the string s as a 2-byte Unicode character (UTF-16) to the stream.

  • It writes characters one-by-one using writeChar(), so each character takes 2 bytes.

  • It's part of the DataOutput interface, which ObjectOutputStream implements.

  • This is raw character writing, not object serialization.

Declaration

Following is the declaration for java.io.ObjectOutputStream.writeChars(String val) method.

public void writeChars(String val)

Parameters

  • val − The String of chars 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 writeChars(String val) method

The following example shows the usage of ObjectOutputStream writeChars(String 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) {
      String s = "Hello";
      
      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.writeChars(s);
         oout.writeChars("World");

         // 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
         for (int i = 0; i < 10; i++) {
            System.out.print("" + ois.readChar());
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

HelloWorld

Example - Write and read a short string using writeChars()

The following example shows the usage of ObjectOutputStream writeChars(String val) method. We're writing the string "Hi" character-by-character 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 = "chars1.bin";

      // Step 1: Write characters
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeChars("Hi");
         System.out.println("Wrote string using writeChars(): Hi");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Step 2: Read characters back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         char c1 = ois.readChar();
         char c2 = ois.readChar();
         System.out.println("Read characters: " + c1 + c2);  // Output: Hi
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote string using writeChars(): Hi
Read characters: Hi

Example - Write multiple strings separately using writeChars()

The following example shows the usage of ObjectOutputStream writeChars(String val) method. We're writing two words using writeChars() and read them back 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 = "chars2.bin";

      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeChars("Hello");
         oos.writeChars("World");
         System.out.println("Wrote: HelloWorld using writeChars()");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read back 10 characters (5 from "Hello", 5 from "World")
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         System.out.print("Read characters: ");
         for (int i = 0; i < 10; i++) {
            System.out.print(ois.readChar());
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote: HelloWorld using writeChars()
Read characters: HelloWorld
java_io_objectoutputstream.htm
Advertisements