Java - Writer write(int c) method



Description

The Java Writer write(int c) method writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

Declaration

Following is the declaration for java.io.Writer.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 Writer write(int c) method

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

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      int c = 70;

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // write an int that will be printed as ASCII
         writer.write(c);

         // flush the writer
         writer.flush();

         // write another int that will be printed as ASCII
         writer.write(71);

         // flush the stream again
         writer.flush();

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

Output

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

FG

Example - Using FileWriter with write(int c)

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

WriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new FileWriter("writeint1.txt");

         writer.write(65); // Writes character 'A' (Unicode value 65)
         writer.write(66); // Writes character 'B'
         writer.write(67); // Writes character 'C'

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

Output

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

Characters written to file.

Explanation

  • Unicode values 65, 66, and 67 correspond to characters 'A', 'B', and 'C'.

  • write(int) writes one character at a time.

  • Output file will contain: ABC.

Example - Using StringWriter with write(int c)

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

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new StringWriter();

         int[] charCodes = {72, 101, 108, 108, 111}; // Corresponds to 'H', 'e', 'l', 'l', 'o'
         for (int code : charCodes) {
            writer.write(code);
         }

         System.out.println("Output: " + writer.toString());  // Prints "Hello"
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Output: Hello

Explanation

  • An array of character codes (Unicode values) is written one by one.

  • StringWriter holds the result in memory, which we print using toString().

java_io_writer.htm
Advertisements