简体   繁体   English

BufferedReaders跨类读取System.in

[英]BufferedReaders reading System.in across classes

I was wondering why I get a java.io.IOException: Stream closed error when using 我想知道为什么我得到一个java.io.IOException: Stream closed使用时java.io.IOException: Stream closed错误

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

In 2 different classes. 在2个不同的班级中。

The setup is as follows. 设置如下。

public class SomeClass{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    //br.readSomeStuff
    br.close();

    new SomeOtherClass(); //defo not passing the br along to the new class!

}
public class SomeOtherClass{

    public SomeOtherClass(){
        method():
    }

    private void method(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
        br.readLine();
        // results into an IOEXCEPTION Stream close
    }

}

The issue is gone when I close the BufferedReader in the first class AFTER the creation of the other class. 当我在创建另一个类之后关闭第一个类中的BufferedReader时,问题就消失了。 I dont understand why this would give issues though. 我不明白为什么这会给问题。 I am creating a new BufferedReader on System.in, why could this possibly result into a stream closed error? 我正在System.in上创建一个新的BufferedReader,为什么这可能导致流关闭错误?

Similar question here . 这里有类似的问题。 Does not explain WHY System.in is closed for some reason though. 尽管由于某些原因,无法解释WHY System.in已关闭。

Thanks in advance! 提前致谢!

Because when you close the BufferedReader all the underlying streams are closed. 因为当您关闭BufferedReader所有基础流都已关闭。 THis is the case with all the classes that wrap and read/write from/to streams. 对于所有包装流以及从流中读取/写入流的类,情况就是如此。

This is a convenience so that you don't need to go through the entire set of objects you've instantiated (the InputStream , the InputStreamReader and finally the BufferedReader ) and close all of them. 这是一种便利,因此您无需遍历实例化的整个对象集( InputStreamInputStreamReader和最后的BufferedReader )并关闭所有对象。

A simple test will demonstrate this: 一个简单的测试将证明这一点:

public static void main(String[] args) throws IOException 
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    br.close();

    // Will throw IOException
    int i = System.in.read();
}

System.in isn't special; System.in并不特殊; it's an InputStream . 这是一个InputStream The same thing would happen if the underlying stream was say, a FileInputStream rather than stdin : 如果说底层流是FileInputStream而不是stdin ,则会发生同样的事情:

public static void main(String[] args) throws IOException 
{
    File f = new File("SomeFileName");
    FileInputStream fis = new FileInputStream(f);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    br.close();

    // throw IOException
    int i = fis.read();
}

Given that usually these constructors are chained (as they are in your example), it would be annoyingly cumbersome to have to retain and close each one. 鉴于通常这些构造函数都是链式的(如您的示例中所示),必须保留并关闭每个构造函数会很烦人。

Imagine having to do the following every time you wanted to use streams: 想象一下,每次要使用流时都必须执行以下操作:

public static void main(String[] args) throws IOException 
{
    File f = new File("SomeFileName");
    FileInputStream fis = new FileInputStream(f);
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(irs);

    // Use the BufferedReader

    br.close();
    isr.close();
    fis.close();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM