本文介绍了错误信息:流关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行下面的代码在$ FlightSearch

Upon running following code under class 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循环,但徒劳无功。

moving out of do-while loop but in vain.

推荐答案

问题



问题是您执行 br.close() ,如javadoc所述,关闭流并释放与之相关的任何系统资源

The problem

The problem is that you execute the br.close() that, as javadoc states, closes the stream and releases any system resources associated with it.

只是为了快速验证注释:

Just for a quick verification comment out the:

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

您可以回答

and you can answer s any times you want without any exception.

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

A solution is closing the buffer reader after all reads are terminated:

    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);
            }
        }
    }

这篇关于错误信息:流关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:41