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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。