流封闭IO异常Java

时间:2016-11-02 22:19:09

标签: java inputstream ioerror

我正在运行以下程序并获得Stream Closed IO Error。 但只在第二个循环中。第一个工作正常。 谁能告诉我为什么? (我检查了文件是否存在而不是空的。)

Some*********************

以下是错误消息:

    private static TimerTask perform(){
        //logging on to FTP-Server

            InputStream  in = client.retrieveFileStream("./htdocs/pwiMain/main.txt");
            InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt");
            BufferedInputStream inbf = new BufferedInputStream(in);
            int bytesRead;
            byte[] buffer = new byte[1024];
            String wholeFile = null;
            String[] contents;
            while((bytesRead = inbf.read(buffer)) != -1){
                wholeFile = new String(buffer,0,bytesRead);
            }
            sentPassword = wholeFile.substring(wholeFile.indexOf("#lap"));
            inbf.close();

            inbf = new BufferedInputStream(pw);
            while((bytesRead = inbf.read(buffer)) != -1){ // this is line72 where the error occurrs...
                wholeFile = new String(buffer,0,bytesRead);
            }
            md5hash = wohleFile;
            inbf.close();
            contents = sentPassword.split("\\r\\n|\\n|\\r");
            System.out.println("contents:    " + contents[0] + "   " + contents[1]);

            //check the password


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("ioexception");
        } finally {

        }
        return null;
    }

非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:3)

你显然不能同时激活两个检索流,这并不奇怪。只需重新排序代码:

private static TimerTask perform(){
    try {
        //logging on to FTP-Server

        InputStream  in = client.retrieveFileStream("./htdocs/pwiMain/main.txt");
        BufferedInputStream inbf = new BufferedInputStream(in);
        int bytesRead;
        byte[] buffer = new byte[1024];
        String wholeFile = null;
        String wholeCred = null;
        String[] contents;
        while((bytesRead = inbf.read(buffer)) != -1){
            wholeFile = new String(buffer,0,bytesRead);
        }
        inbf.close();  // ADDED

        InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt");
        BufferedInputStream pwbf = new BufferedInputStream(pw);
        int pwBytesRead; // YOU DON'T NEED THIS, you could reuse the previous one
        byte[] pwBuffer = new byte [1024]; // DITTO
        while((pwBytesRead = pwbf.read(pwBuffer)) != -1){
            wholeCred = new String(pwBuffer,0,pwBytesRead);
        }
        pwbf.close(); // ADDED

        sentPassword =  wholeFile.substring(sentPassword.indexOf("#lap"));
        md5hash = wholeCred;
        contents = sentPassword.split("\\r\\n|\\n|\\r");
        System.out.println("contents:    " + contents[0] + "   " + contents[1]);

        //check the password


    } catch (IOException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("ioexception");
    } finally {

    }
    return null;
}

现在你的方式没有任何意义或优势,你只是在浪费空间,而且你发现它不起作用。

当然,您会发现,如果其中一个输入超过一个缓冲区,它将无效,但您没有问过这个问题。