描述
這個java.io.ObjectInputStream.close()方法關閉輸入流。必須調用以釋放與流關聯的任何資源。
聲明
以下是聲明java.io.ObjectInputStream.close()方法。
public void close()
參數
NA
返回值
此方法不返回值。
異常
IOException- 如果發生了 I/O 錯誤。
示例
下麵的例子展示了使用java.io.ObjectInputStream.close()方法。
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();
}
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果——
Hello World Closing Stream... Stream Closed.
相關用法
- Java Java.io.ObjectInputStream.readObject()用法及代碼示例
- Java Java.io.ObjectInputStream.getField.get()用法及代碼示例
- Java Java.io.ObjectInputStream.readInt()用法及代碼示例
- Java Java.io.ObjectInputStream.readFloat()用法及代碼示例
- Java Java.io.ObjectInputStream.readChar()用法及代碼示例
- Java Java.io.ObjectInputStream.readUTF()用法及代碼示例
- Java Java.io.ObjectInputStream.skipBytes()用法及代碼示例
- Java Java.io.ObjectInputStream.resolveClass()用法及代碼示例
- Java Java.io.ObjectInputStream.available()用法及代碼示例
- Java Java.io.ObjectInputStream.readBoolean()用法及代碼示例
- Java Java.io.ObjectInputStream.read()用法及代碼示例
- Java Java.io.ObjectInputStream.readUnshared()用法及代碼示例
- Java Java.io.ObjectInputStream.readLong()用法及代碼示例
- Java Java.io.ObjectInputStream.readDouble()用法及代碼示例
- Java Java.io.ObjectInputStream.readFields()用法及代碼示例
- Java Java.io.ObjectInputStream.readShort()用法及代碼示例
- Java Java.io.ObjectInputStream.resolveObject()用法及代碼示例
- Java Java.io.ObjectInputStream.readFully()用法及代碼示例
- Java Java.io.ObjectInputStream.readByte()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeObject()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.io.ObjectInputStream.close() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。