Java - ObjectStreamWriter close() method



Description

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

public void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of ObjectStreamWriter close() method

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         // create a new OutputStreamWriter
         OutputStream os = new FileOutputStream("test.txt");
         OutputStreamWriter writer = new OutputStreamWriter(os);

         // create a new FileInputStream to read what we write
         FileInputStream in = new FileInputStream("test.txt");

         // write something in the file
         writer.write(70);

         // flush the stream
         writer.flush();

         // read what we write
         System.out.println("" + (char) in.read());

         // close the stream
         System.out.println("Closing Stream...");
         writer.close();
         System.out.println("Stream closed.");
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

F
Closing Stream...
Stream closed.

Example - Using close() to release file resources

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         FileOutputStream fos = new FileOutputStream("output_writer1.txt");
         OutputStreamWriter writer = new OutputStreamWriter(fos);

         writer.write("This is written using OutputStreamWriter.");

         // Close the writer to flush and release resources
         writer.close();

         System.out.println("Data written and stream closed.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written and stream closed.

Explanation

  • close() flushes any buffered characters and closes the stream.

  • It's essential to prevent data loss and free system resources.

  • If close() is not called, some characters may remain in the buffer and not be written to the file.

Example - Using close() in a finally block to ensure cleanup

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      OutputStreamWriter writer = null;

      try {
         writer = new OutputStreamWriter(new FileOutputStream("output_writer2.txt"));
         writer.write("This text will be safely written and stream closed.");
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         // Ensure stream is closed even if an exception occurs
         try {
            if (writer != null) {
               writer.close();
               System.out.println("Writer closed in finally block.");
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Output

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

Writer closed in finally block.

Explanation

  • close() is placed in the finally block to guarantee it runs regardless of errors.

  • This is good practice to prevent resource leaks when working with I/O.

  • Even if write() throws an exception, close() will still be executed.

java_io_outputstreamwriter.htm
Advertisements