当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java PushbackInputStream close()用法及代码示例


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()




相关用法


注:本文由纯净天空筛选整理自pp_pankaj大神的英文原创作品 PushbackInputStream close() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。