ObjectInputStream close() method in Java with examples
Last Updated :
28 May, 2019
The close() method of the ObjectInputStream class in Java closes the input stream.
Syntax:
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:
import java.io.*;
public class GFG {
public static void main(String[] args) throws Exception
{
try {
FileOutputStream out
= new FileOutputStream( "gopal.txt" );
ObjectOutputStream out1
= new ObjectOutputStream(out);
out1.writeUTF( "Geeks for Geeks" );
out1.flush();
ObjectInputStream example
= new ObjectInputStream(
new FileInputStream( "gopal.txt" ));
for ( int i = 0 ; i < example.available();) {
System.out.println( "" + ( char )example.read());
}
System.out.println( "\nClosing Stream..." );
example.close();
System.out.println( "Stream Closed." );
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
Output:
Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#close()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...