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


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


Java中的PushbackReader类的close()方法用于关闭流并释放流中繁忙的资源(如果有)。此方法具有以下结果:

  • 如果流是打开的,则关闭流以释放资源
  • 如果流已经关闭,则将无效。
  • 如果对流执行任何读取或其他类似操作,则在关闭后,将引发IOException

用法:

public void close()

参数:此方法不接受任何参数


返回值:此方法不返回任何值。

异常:如果输入输出时发生某些错误,则此方法将引发IOException。

下面的方法说明了close()方法的用法方式:

程序1:

// Java program to demonstrate 
// PushbackReader close() method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
            String s = "GeeksForGeeks"; 
  
            // Initializing a StringReader and PushbackReader 
            StringReader stringReader 
                = new StringReader(s); 
            PushbackReader pushbackReader 
                = new PushbackReader(stringReader); 
  
            System.out.println("Is stream ready: "
                               + pushbackReader.ready()); 
  
            // Close the stream using close() 
            pushbackReader.close(); 
            System.out.println("Stream Closed."); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
Is stream ready: true
Stream Closed.

程序2:

// Java program to demonstrate 
// PushbackReader close() method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
            // Initializing a StringReader and PushbackReader 
            String s = "GFG"; 
  
            StringReader stringReader 
                = new StringReader(s); 
            PushbackReader pushbackReader 
                = new PushbackReader(stringReader); 
  
            System.out.println("Is stream ready: "
                               + pushbackReader.ready()); 
  
            // Close the stream using close() 
            pushbackReader.close(); 
            System.out.println("Stream Closed."); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
Is stream ready: true
Stream Closed.

参考: https://docs.oracle.com/javase/9/docs/api/java/io/PushbackReader.html#close-



相关用法


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