Java - FilterWriter write(int c) method



Description

The Java FilterWriter write(int c) method writes a single character to the output stream. The integer parameter represents the Unicode code point of a character. The integer c is converted to a character before writing. Inefficient for writing large amounts of text (use write(char[]) or write(String) instead).

Declaration

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

public void write(int c)

Parameters

  • c − integer specifying the source character to be written out to this filter writer.

Return Value

The method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterWriter write(int c) method

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

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterDemo {
   public static void main(String[] args) throws Exception {
      FilterWriter fw = null;
      Writer w = null;
      String s = null;
      
      try {
         // create new reader
         w = new StringWriter(6);
          
         // filter writer
         fw = new FilterWriter(w) {
         };
         
         // write to filter writer
         fw.write(65);
         fw.write(66);
         fw.write(67);

         // get the string
         s = w.toString();
         
         // print
         System.out.print("String: "+s);
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(w!=null)
            w.close();
         if(fw!=null)
            fw.close();
      }
   }
}

Output

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

String: ABC

Example - Writing Individual Characters Using BufferedWriter

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

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (FilterWriter fw = new FilterWriter(new FileWriter("output.txt")){}) {
         fw.write(72); // ASCII for 'H'
         fw.write(101); // ASCII for 'e'
         fw.write(108); // ASCII for 'l'
         fw.write(108); // ASCII for 'l'
         fw.write(111); // ASCII for 'o'

         System.out.println("Characters written successfully to output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Characters written successfully to output.txt.

Explanation

  • Uses BufferedWriter, a subclass of FilterWriter.

  • Writes characters one by one using their ASCII values

    • 72 → 'H'

    • 101 → 'e'

    • 108 → 'l'

    • 108 → 'l'

    • 111 → 'o'

  • The file stores these characters as text.

Example - Writing Unicode Characters Using FileWriter

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

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (FilterWriter fw = new FilterWriter(new FileWriter("unicode_output.txt")){}) {
         fw.write(61); // Unicode for '='
         fw.write(62); // Unicode for '>' 
         fw.write(63); // Unicode for '?' 

         System.out.println("Unicode characters written successfully to unicode_output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }    
   }
}

Output

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

Unicode characters written successfully to unicode_output.txt.

Explanation

  • Uses PrintWriter, which extends FilterWriter.

  • The file stores these Unicode symbols as text.

java_io_filterwriter.htm
Advertisements