Java - ObjectOutputStream close() method



Description

The Java ObjectOutputStream close() method closes the stream. This method must be called to release any resources associated with the stream.

Declaration

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

public void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error has occurred.

Example - Usage of ObjectOutputStream close() method

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

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      int i = 319874;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeInt(i);
         oout.writeInt(1653984);
         oout.flush();

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print an int
         System.out.println("" + ois.readInt());

         // read and print an int
         System.out.println("" + ois.readInt());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

319874
1653984

Example - Properly closing ObjectOutputStream

The following example shows the usage of ObjectOutputStream close() method. We're properly closing ObjectOutputStream after writing an object.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "person1.ser";

      try {
         FileOutputStream fos = new FileOutputStream(filename);
         ObjectOutputStream oos = new ObjectOutputStream(fos);

         Person person = new Person("Alice", 30);
         oos.writeObject(person);

         // Close the stream
         oos.close();
         System.out.println("ObjectOutputStream closed after writing object.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   static class Person implements Serializable {
      String name;
      int age;
      Person(String name, int age) {
         this.name = name;
         this.age = age;
      }
   }
}

Output

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

ObjectOutputStream closed after writing object.

Explanation

  • We're serializing a Person object and explicitly calls close() on the ObjectOutputStream.

  • Closing the stream flushes the data and releases system resources.

Example - Use of close() method

The following example shows the usage of ObjectOutputStream close() method. We'll write extra info to the stream (e.g. version info).

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      ObjectOutputStream oos = null;

      try {
         // Create a FileOutputStream to write to a file
         FileOutputStream fos = new FileOutputStream("data.ser");

         // Create an ObjectOutputStream to write objects
         oos = new ObjectOutputStream(fos);

         // Create an object to serialize
         String message = "Hello, Java Serialization!";

         // Write the object to the stream
         oos.writeObject(message);

         System.out.println("Object written to file successfully.");

      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         // Close the ObjectOutputStream in finally block to ensure it's always closed
         if (oos != null) {
            try {
               oos.close(); // Closing the ObjectOutputStream
               System.out.println("ObjectOutputStream closed successfully.");
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}

Output

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

Object written to file successfully.
ObjectOutputStream closed successfully.

Explanation

  • ObjectOutputStream This class is used to write primitive data types and objects to an OutputStream.

  • close() method

    • The close() method closes the stream and releases any system resources associated with it.

    • It's important to always close streams to:

      • Free up system resources

      • Ensure all buffered data is properly written

      • Prevent resource leaks

java_io_objectoutputstream.htm
Advertisements