Java - Writer close() method



Description

The Java Writer close() method 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.

Declaration

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

public abstract void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of Writer close() method

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

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // append a string
         writer.append(s);

         // flush the writer
         writer.flush();

         // append a new string
         writer.append("\nThis is an example");

         // flush and close the stream
         writer.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello World
This is an example

Example - Using FileWriter with close()

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

WriterDemo.java

package com.tutorialspoint;

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

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new FileWriter("example1.txt");
         writer.write("Hello, this is a test.");
         writer.close(); // Closing the writer to release resources
         System.out.println("File written and writer closed.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

File written and writer closed.

Explanation

  • A FileWriter writes text to a file.

  • After writing, writer.close() is called to−

    • Flush the stream (if needed),

    • Release file locks and system resources,

    • Prevent memory leaks.

  • Writing again after close() will throw an exception.

Example - Closing BufferedWriter

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

WriterDemo.java

package com.tutorialspoint;

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

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new BufferedWriter(new FileWriter("example2.txt"));
         writer.write("Buffered writing example.");
         writer.close(); // Automatically closes BufferedWriter and FileWriter
         System.out.println("BufferedWriter closed.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

BufferedWriter closed.

Explanation

  • BufferedWriter wraps FileWriter to improve efficiency.

  • The close() method ensures −

    • The buffered data is flushed to the file.

    • Both the BufferedWriter and underlying FileWriter are closed.

  • This avoids resource leaks and ensures all data is saved.

java_io_writer.htm
Advertisements