Java - FilterWriter close() method



Description

The Java FilterWriter close() method closes the writer and releases system resources associated with it. Flushes any remaining buffered data before closing. Once closed, the writer cannot be used again.

Declaration

Following is the declaration for java.io.FilterWriter.close() method −

public void close()

Parameters

NA

Return Value

The method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterWriter close() method

The following example shows the usage of Java FilterWriter close() 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);
         
         // 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 system resources associated with this stream
         if(w!=null)
            w.close();
         if(fw!=null) {
            fw.close();
            System.out.println("close() invoked");
            System.out.print("flushes out and then closes the stream");
         }
      }
   }
}

Output

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

String: A
close() invoked
flushes out and then closes the stream

Example - Using close() with BufferedWriter (Automatic Closure)

The following example shows the usage of Java FilterWriter close() method.

FilterWriterDemo.java

package com.tutorialspoint;

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

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (BufferedWriter fw = new BufferedWriter(new FileWriter("output.txt"))) {
         fw.write("Hello, FilterWriter!");
         System.out.println("Data written successfully.");
         // No need to manually close, try-with-resources handles it
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written successfully.

Explanation

  • Uses BufferedWriter, which is a subclass of FilterWriter.

  • Writes "Hello, FilterWriter!" to "output.txt".

  • Uses try-with-resources, which automatically calls close() at the end.

Example - Manually Closing PrintWriter (Another FilterWriter Subclass)

The following example shows the usage of Java FilterWriter close() method.

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FilterWriterDemo {
   public static void main(String[] args) {
      PrintWriter fw = null;
      try {
         fw = new PrintWriter(new FileWriter("data.txt"));
         fw.write("Java FilterWriter Example");
         System.out.println("Data written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (fw != null) {
		   fw.close(); // Manually closing the stream
		   System.out.println("Stream closed successfully.");
		}
      }
   }
}

Output

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

Data written to file.
Stream closed successfully.

Explanation

  • Uses PrintWriter, which extends FilterWriter.

  • Writes "Java FilterWriter Example" to "data.txt".

  • Manually closes the writer inside finally to ensure resources are freed.

  • Prevents memory leaks by checking if (fw != null) before calling close().

java_io_filterwriter.htm
Advertisements