Java中的PushbackInputStream類的close()方法用於關閉輸入流,並釋放與該流關聯的係統資源。調用此方法後,如果此類將引發IOException,則進一步調用其他方法。
用法:
public void close() throws IOException
指定者:該方法由close()方法AutoCloseable接口和close()方法Closeable接口指定。
覆蓋:此方法覆蓋FilterInputStream類的close()方法。
參數:此方法不接受任何參數。
返回值:此方法不返回任何值。
異常:如果發生I /O錯誤,則此方法將引發IOException。
以下示例程序旨在說明IO包中的PushbackInputStream類的close()方法:
程序1:
// Java program to illustrate
// PushbackInputStream close() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
try {
// Create an array
byte[] byteArray
= new byte[] { 'G', 'E', 'E',
'K', 'S' };
// Create inputStream
InputStream inputStr
= new ByteArrayInputStream(byteArray);
// Create object of
// PushbackInputStream
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
for (int i = 0; i < byteArray.length; i++) {
// Read the character
System.out.print(
(char)pushbackInputStr.read());
}
// Revoke close()
pushbackInputStr.close();
}
catch (Exception e) {
System.out.println("Stream is closed");
}
}
}
輸出:
GEEKS
程序2:
// Java program to illustrate
// PushbackInputStream close() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
try {
// Create an array
byte[] byteArray
= new byte[] { 'G', 'E', 'E',
'K', 'S' };
// Create inputStream
InputStream inputStr
= new ByteArrayInputStream(byteArray);
// Create object of
// PushbackInputStream
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
// Revoke close()
pushbackInputStr.close();
for (int i = 0; i < byteArray.length; i++) {
// Read the character
System.out.print(
(char)pushbackInputStr.read());
}
}
catch (Exception e) {
System.out.println("Stream is closed");
}
}
}
輸出:
Stream is closed
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#close()
相關用法
- Java PushbackInputStream available()用法及代碼示例
- Java PushbackInputStream unread()用法及代碼示例
- Java PushbackInputStream mark()用法及代碼示例
- Java PushbackInputStream markSupported()用法及代碼示例
- Java PushbackInputStream reset()用法及代碼示例
- Java PushbackInputStream skip()用法及代碼示例
- Java PushbackInputStream read()用法及代碼示例
- Java ByteArrayInputStream close()用法及代碼示例
- Java Scanner close()用法及代碼示例
- Java CharArrayReader close()用法及代碼示例
- Java BufferedWriter close()用法及代碼示例
- Java ObjectInputStream close()用法及代碼示例
- Java ByteArrayOutputSteam close()用法及代碼示例
- Java BufferedInputStream close()用法及代碼示例
- Java PushbackReader close()用法及代碼示例
- Java Reader close()用法及代碼示例
- Java Formatter close()用法及代碼示例
- Java PrintStream close()用法及代碼示例
- Java CharArrayWriter close()用法及代碼示例
- Java StringWriter close()用法及代碼示例
注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 PushbackInputStream close() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。