Home Java Example Java Io Java IO FilterWriter

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO FilterWriter

Advertisement
In this example we will discuss about the FilterWriter in Java.

Java IO FilterWriter

In this example we will discuss about the FilterWriter in Java.

java.io.FilterWriter is an abstract class allows to write the filtered character streams. Instance of this class can not be created directly. Subclasses of FilterWriter may override methods of this class and may allow extra methods and fields.

Constructor Detail

Constructor
Description
FilterWriter(Writer out) This constructor is used to create a new FilterWriter.
Syntax : protected FilterWriter(Writer out)

Method Detail

  • close() : This method is used to close the stream but it flush the stream first.

    Syntax : public void close() throws IOException
     
  • flush() : This method is used to flush the stream.

    Syntax : public void flush() throws IOException
     
  • write(char[] cbuf, int off, int len) : This method is used to write the specified length of characters started from the specified offset of an array of characters to the stream.

    Syntax : public void write(char[] cbuf, int off, int len) throws IOException
     
  • write(int c) : This method is used to write a character to the stream.

    Syntax : write(int c)
     
  • write(String str, int off, int len) : This method is used to write the specified length of string started from the specified offset of a string to the stream.

    Syntax : public void write(String str, int off, int len) throws IOException

Example

Here an example is being given which demonstrates about how to use Java FilterWriter. In this example I have created a Java class into which tried to write the data into the output stream. In this example I have used the various of Java classes of IO package such as BufferedWriter (to write into the stream from buffer), FileWriter( created a file for writing the data) etc.

Source Code

JavaFileWriterExample.java

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

class JavaFilterWriterExample extends FilterWriter
{
     
   public JavaFilterWriterExample(Writer wr)
     {
        super(wr);
     }

   public static void main(String[] args)
    {
      FileWriter fw = null;
      BufferedWriter bw = null;
            
      try
       {
         fw = new FileWriter("file.txt");
         bw = new BufferedWriter(fw);
         System.out.println();
         bw.write("Hello");
         bw.newLine();
         bw.write("Java IO FilterWriter Example");
         System.out.println("Data is written successfully into the file.");
       }
      catch(IOException e)
       {
          System.out.println(e);
       }
      finally
       {
            if(fw != null)
              {
                 try
                   {
                      bw.flush();
	        bw.close();
                      fw.close();
                   }
                  catch(Exception e)
                   {
                       System.out.println(e);
                   }
               }
         }// end finally
      }// end main
  }// end class

Output

When you will execute the above example you will get the output as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 20, 2012

Related Tutorials

Discuss: Java IO FilterWriter  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com