Skip to content
Related Articles

Related Articles

ObjectInputStream close() method in Java with examples

View Discussion
Improve Article
Save Article
  • Last Updated : 28 May, 2019

The close() method of the ObjectInputStream class in Java closes the input stream.


public void close()

Parameters: This method does not accept any parameter.

Return Value: This method does not returns anything.

Below program illustrate the above method:

Program 1:

// Java program to illustrate 
// the above method
public class GFG {
    public static void main(String[] args) throws Exception
        try {
            // Creates a new file
            // with an ObjectOutputStream
            FileOutputStream out
                = new FileOutputStream("gopal.txt");
            ObjectOutputStream out1
                = new ObjectOutputStream(out);
            // write in the file
            out1.writeUTF("Geeks for Geeks");
            // FLushes the stream
            // Create an ObjectInputStream
            // for the file we created before
            ObjectInputStream example
                = new ObjectInputStream(
                    new FileInputStream("gopal.txt"));
            // read from the stream
            for (int i = 0; i < example.available();) {
                System.out.println("" + (char);
            // closes the stream
            System.out.println("\nClosing Stream...");
            // Stream closed
            System.out.println("Stream Closed.");
        catch (Exception ex) {



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!