Java - ObjectStreamWriter write(int c) method



Description

The Java ObjectStreamWriter write(int c) method method writes a single character.

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the OutputStreamWriter(OutputStream, String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.method flushes the stream.

Declaration

Following is the declaration for java.io.ObjectStreamWriter.write(int c) method.

public void write(int c)

Parameters

c − int specifying a character to be written.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of ObjectStreamWriter write(int c) method

The following example shows the usage of ObjectStreamWriter write(int c) method.

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         // create a new OutputStreamWriter
         OutputStream os = new FileOutputStream("test.txt");
         OutputStreamWriter writer = new OutputStreamWriter(os);

         // create a new FileInputStream to read what we write
         FileInputStream in = new FileInputStream("test.txt");

         // write something in the file
         writer.write(70);
         writer.write(71);
         writer.write(72);

         // flush the stream
         writer.flush();

         // read what we write
         for (int i = 0; i < 3; i++) {
            System.out.print("" + (char) in.read());
         }

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

FGH

Example - Writing a single character to a file

The following example shows the usage of ObjectStreamWriter write(int c) method. We're printing whether fields in a Document class are marked unshared.

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("single_char1.txt"));

         writer.write(65);  // ASCII value of 'A'

         writer.close();
         System.out.println("Character 'A' written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Character 'A' written to file.

Explanation

  • write(int c) writes a single character.

  • Although the method accepts an int, only the lower 16 bits (as a char) are used.

  • In this case, 65 is the ASCII code for 'A', which is written to the file.

Example - Writing multiple characters individually using write(int c)

The following example shows the usage of ObjectStreamWriter write(int c) method.

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try (OutputStreamWriter writer = new OutputStreamWriter(System.out)) {
         int[] chars = {72, 101, 108, 108, 111}; // ASCII for 'H', 'e', 'l', 'l', 'o'

         for (int c : chars) {
            writer.write(c);  // Write each character one by one
         }

         writer.write('\n');  // Move to new line
         writer.flush();      // Ensure output appears on screen
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Hello

Explanation

  • Each int in the array corresponds to a character in the word "Hello".

  • write(int c) is called for each character.

  • This method is useful when processing or filtering characters one at a time before writing.

java_io_outputstreamwriter.htm
Advertisements