Java.io.FilterWriter class in Java

Abstract class for writing filtered character streams. The abstract class FilterWriter itself provides default methods that pass all requests to the contained stream. Subclasses of FilterWriter should override some of these methods and may also provide additional methods and fields.
Constructor :

  • protected FilterWriter(Writer out) : Create a new filtered writer.

Methods :

  • void close() : Closes the stream, flushing it first.Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.
    Syntax :public void close()
               throws IOException
    Throws:
    IOException 
  • void flush() : Flushes the stream.
    Syntax :public void flush()
               throws IOException
    Throws:
    IOException
  • void write(char[] cbuf, int off, int len) : Writes a portion of an array of characters.
    Syntax :public void write(char[] cbuf,
             int off,
             int len)
               throws IOException
    Parameters:
    cbuf - Buffer of characters to be written
    off - Offset from which to start reading characters
    len - Number of characters to be written
    Throws:
    IOException
  • void write(int c) : Writes a single character.
    Syntax :public void write(int c)
               throws IOException
    Parameters:
    c - int specifying a character to be written
    Throws:
    IOException
  • void write(String str, int off, int len) : Writes a portion of a string.
    Syntax :public void write(String str,
             int off,
             int len)
               throws IOException
    Parameters:
    str - String to be written
    off - Offset from which to start reading characters
    len - Number of characters to be written
    Throws:
    IOException 

Program :



filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program demonstrating FilterWriter methods
import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;
class FilterWriterDemo
{
    public static void main(String[] args) throws Exception
    {
        FilterWriter fr = null;
        Writer wr = null;
        wr = new StringWriter();
        fr = new FilterWriter(wr) {} ;
        String str = "Geeksfor";
        char c[] = {'G','e','e','k'};
  
        //illustrating write(String str,int off,int len)
        fr.write(str);
          
        //illustrating flush()
        fr.flush();
  
        //illustrating write(char[] cff,int off,int len)
        fr.write(c);
  
        //illustrating write(int c)
        fr.write('s');
        System.out.print(wr.toString());
  
        //close the stream
        fr.close();
    }
}

chevron_right


Output :

GeeksforGeeks

This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

java-img




My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.