Java.io.ObjectInputStream.close() Method
Advertisements
Description
The java.io.ObjectInputStream.close() method closes the input stream. Must be called to release any resources associated with the stream.
Declaration
Following is the declaration for java.io.ObjectInputStream.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
The following example shows the usage of java.io.ObjectInputStream.close() method.
package com.tutorialspoint;
import java.io.*;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
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.writeUTF("Hello World");
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
// read from the stream
for (int i = 0; i < ois.available();) {
System.out.print("" + (char) ois.read());
}
// change line
System.out.println();
// close the stream
System.out.println("Closing Stream...");
ois.close();
System.out.println("Stream Closed.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
Hello World Closing Stream... Stream Closed.