错误消息:流已关闭

时间:2014-03-24 13:40:16

标签: java error-handling bufferedreader ioexception

在类FlightSearch下运行以下代码

String moreSearch = "y";
    List<Flight> resultList;

    // load initial flight data into DB
    if (!init()) {
        return;
    }
    // A background thread to monitor changes in csv repository
    FileListner fl = new FileListner();
    fl.start();

    do {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        // main thread gets input
        Input inputQuery = new Input();
        try {
            inputQuery.getQuery();
        } catch (InvalidException e1) {
            e1.getMessage();
        } finally {
            fl.stopThread();
        }
        // main thread STARTs processing task as background monitors csv
        // repo
        QueryProcessor processor = new QueryProcessor();
        resultList = null;
        resultList = processor.matchQuery(inputQuery);

        displayResult(resultList, inputQuery.getFlightClass());

        System.out
                .println("More Flight Query ? Press n/N to exit. Anyother key to continue searching.");

        try {
            moreSearch = br.readLine(); // LINE 56
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    } while (!moreSearch.equalsIgnoreCase("n"));

    System.out.println("Thank You !!!");

我收到以下错误:

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.read(BufferedInputStream.java:325)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at com.myApp.FlightSearch.main(FlightSearch.java:56)

我也尝试过移动

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

退出do-while循环,但徒劳无功。

3 个答案:

答案 0 :(得分:6)

问题

问题是你执行br.close(),就像javadoc所说,关闭流并释放与之相关的所有系统资源

只需快速验证即可:

if (br != null) {
//    try {
//        br.close();
//    } catch (IOException e) {
//        // TODO Auto-generated catch block
//        e.printStackTrace();
//    }
}

您可以随时回答s,无任何例外。

解决方案

解决方案是在所有读取终止后关闭缓冲读取器:

    String moreSearch = null;
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    try {
        do {
            // ...
            System.out.println("More Flight Query ? Press n/N to exit. Anyother key to continue searching.");
            moreSearch = br.readLine();
        } while (!moreSearch.equalsIgnoreCase("n"));

    } catch (IOException e) {
        Logger.getLogger(FlightSearch.class.getName()).log(Level.SEVERE, "Cant read line from a System.in based BufferedReader", e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ignoreMe) {
                Logger.getLogger(FlightSearch.class.getName()).log(Level.SEVERE, "Can't close a System.in based BufferedReader", ignoreMe);
            }
        }
    }

答案 1 :(得分:0)

您的问题是,您正在br.close()块中调用finally,从缓冲区中读取一行。当您关闭Stream时,由您关闭的流包裹的任何流也将关闭。因此,您在do { } while ()循环中的第一次迭代正在关闭System.in InputStream,然后您无法重新打开。

为了避免这种情况,您可以使用来自Apache Commons的CloseShieldInputStream来包装System.in,然后再使用InputStreamReader进行包装。这样您就可以关闭BufferedReader(也会关闭InputStreamReaderCloseShieldInputStream),而不会触发System.in的关闭。

请参阅bmargulies答案:Closing BufferedReader and System.in

答案 2 :(得分:0)

根据previous answer中的建议进行更改后,我的代码没有进行调整,但如下所示略有更改,我的代码编译并运行得很好

    try {
        do {
            // main thread gets input
            Input inputQuery = new Input();
            inputQuery.getQuery();

            // main thread STARTs processing task as background monitors csv
            // repo
            QueryProcessor processor = new QueryProcessor();
            resultList = null;
            resultList = processor.matchQuery(inputQuery);

            displayResult(resultList, inputQuery.getFlightClass());
            System.out
                    .println("More Flight Query ? Press n/N to exit. Anyother key to continue searching.");
            moreSearch = br.readLine();

        } while (!moreSearch.equalsIgnoreCase("n"));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidException e1) {
        e1.getMessage();
    } finally {
        fl.stopThread();
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println("Thank You !!!");
}

感谢您的帮助。