Java PushbackReader close()方法
java.io.PushbackReader.close() 方法關閉該流并釋放與之關聯的所有系統資源。一旦流已關閉,進一步的read(), unread(), ready(), 或skip() 調用将抛出IOException。
1 文法
public void close()
2 參數
無
3 傳回值
無
4 示例
package com.yiidian;
import java.io.*;
public class Demo {
public static void main(String[] args) {
String s = "Hello World";
// create a new StringReader
StringReader sr = new StringReader(s);
// create a new PushBack reader based on our string reader
PushbackReader pr = new PushbackReader(sr, 20);
try {
// read the first five chars
for (int i = 0; i < 5; i++) {
char c = (char) pr.read();
System.out.print("" + c);
}
// change line
System.out.println();
System.out.println("Closing stream...");
// close the stream
pr.close();
System.out.println("Stream closed.");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
輸出結果為:
Hello
Closing stream...
Stream closed.