Java.io.Writer.write() Method



Description

The java.io.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() 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

The following example shows the usage of java.io.Writer.write() method.

package com.tutorialspoint;

import java.io.*;

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();
      }
   }
}

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

FG
java_io_writer.htm
Advertisements