Java中的ObjectInputStream类的close()方法关闭输入流。
用法:
public void close()
参数:此方法不接受任何参数。
返回值:此方法不返回任何内容。
以下示例程序旨在说明上述方法:
示例1:
// Java program to illustrate
// the above method
import java.io.*;
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
out1.flush();
// 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)example.read());
}
// closes the stream
System.out.println("\nClosing Stream...");
example.close();
// Stream closed
System.out.println("Stream Closed.");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
参考: https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#close()
相关用法
- Java ObjectInputStream available()用法及代码示例
- Java ObjectInputStream read()用法及代码示例
- Java ObjectInputStream readBoolean()用法及代码示例
- Java Writer close()用法及代码示例
- Java PrintWriter close()用法及代码示例
- Java Scanner close()用法及代码示例
- Java CharArrayReader close()用法及代码示例
- Java Formatter close()用法及代码示例
- Java ByteArrayInputStream close()用法及代码示例
- Java CharArrayWriter close()用法及代码示例
- Java Reader close()用法及代码示例
- Java StringWriter close()用法及代码示例
- Java StringReader close()用法及代码示例
- Java PrintStream close()用法及代码示例
- Java PushbackReader close()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 ObjectInputStream close() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。