Java.io - SPLessons

Java.io FilterWriter

Home > Lesson > Chapter 21
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FilterWriter

Java.io FilterWriter

shape Introduction

The Java.io FilterWriter is the class used to write the characters which are filtered. The filter writer is subclass to the writer class and overrides all the methods from the writer class. In filter writer class also have its default methods.

Class Declaration

shape Declaration

Java.io FilterWriter class is declared below: Public abstract class Java.io FilterWriter extends Writer

shape Fields

Java.io FilterWriter class fields are:
  • Protected Writer out : The field represents the output stream have to write.
  • Protected Object Lock : For synchronize operations this object is used in FilterWriter.

Class Constructors

shape Table

Constructor Description
Protected FilterWriter(Writer out) The function of this constructor is to create the filer writer instance and used to write from output stream.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
void flush () The function of this method is to flush the specified output stream.
void write (int c) The function of this method is to write the represented character into output stream.
void write (Char[] cbuf, int off, int n) By using this method it writes the data up to ‘n’ length from the output stream to an array of Characters.

Inherited Methods

shape Description

From the following classes, methods are inherited to the filter writer class.
  • Java.io.Object
  • Java.io.Writer

shape Examples

Usage of void close() method. [c]import java.io.FilterWriter; import java.io.StringWriter; import java.io.Writer; public class FilterWriter { public static void main(String[] args) throws Exception { FilterWriter fw = null; Writer w = null; String s=null; try{ // creating a new reader w = new StringWriter(6); // creating a new filter writer fw = new FilterWriter(w) { }; // write to filter writer fw.write(83); // To get the string s = w.toString(); // print System.out.println("String: "+s); }catch(Exception e) { // if any I/O error occurs e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(w!=null) w.close(); if(fw!=null) { fw.close(); System.out.println("close() method accessed"); System.out.print("flushes out then closes the stream"); } } } }[/c] Output The result will be as follows. [c] String: S close() method accessed flushes out then closes the stream[/c] Usage of void write (Char[] cbuf, int off, int n) method [c]import java.io.FilterWriter; import java.io.StringWriter; import java.io.Writer; public class FilterWriter { public static void main(String[] args) throws Exception { FilterWriter fw = null; Writer w = null; char[] cbuf={'S','P','L','E','S','S', 'O', 'N', 'S'}; String s=null; try{ // creating a new string writer w = new StringWriter(6); // creating a new filter writer fw = new FilterWriter(w) { }; // write to the filter writer from character buffer fw.write(cbuf, 2, 4); // To get the string s = w.toString(); // To print System.out.print("String: "+s); }catch(Exception e) { // If any I/O error occurs e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(w!=null) w.close(); if(fw!=null) fw.close(); } } }[/c] Output The result will be as follows. [c]String: LESS[/c]

Summary

shape Key Points

  • The filter writer is the class used to write the characters which are filtered.
  • The filter writer is subclass to the writer class and overrides all the methods from the writer class.