Java - OutputStream close() method



Description

The Java OutputStream close() method closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened. The close method of OutputStream does nothing.

  • close() releases system resources (like file handles, sockets, etc.) associated with the output stream.

  • After calling close(), the stream can no longer be used.

  • It's critical to avoid resource leaks and ensure proper file flushing and cleanup.

Declaration

Following is the declaration for java.io.OutputStream.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 OutputStream close() method

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

OutputStreamDemo.java

package com.tutorialspoint;

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

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

         // craete a new input stream
         InputStream is = new FileInputStream("test.txt");

         // write something
         os.write('A');

         // flush the stream
         os.flush();

         // close the stream but it does nothing
         os.close();

         // read what we wrote
         System.out.println("" + (char) is.read());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

A 

Example - Writing to a file using FileOutputStream and closing it

The following example shows the usage of OutputStream close() method. We're writing data to a file, then explicitly close the stream.

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamDemo {
   public static void main(String[] args) {
      OutputStream os = null;
      try {
         os = new FileOutputStream("example1.txt");
         String data = "Hello, OutputStream!";
         os.write(data.getBytes());
         System.out.println("Data written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if (os != null) {
               os.close(); // important to close the stream
               System.out.println("Stream closed.");
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Output

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

Data written to file.
Stream closed.

Explanation

  • os.close() ensures the file is properly saved and the resource is released.

  • Not closing could lead to data not being written or file locks.

Example - Using ByteArrayOutputStream and closing it (optional but safe)

The following example shows the usage of OutputStream close() method. We're using close() with an in-memory stream (ByteArrayOutputStream).

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class OutputStreamDemo {
   public static void main(String[] args) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      try {
         baos.write("In-memory data".getBytes());
         System.out.println("Byte array content: " + baos.toString());
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            baos.close(); // no harm; good practice
            System.out.println("ByteArrayOutputStream closed.");
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Output

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

Byte array content: In-memory data
ByteArrayOutputStream closed.

Explanation

  • ByteArrayOutputStream.close() doesn't have real effect but is good habit, especially when working with OutputStream polymorphically.

  • Keeps your resource-handling consistent and safe for future code changes.

java_io_outputstream.htm
Advertisements