gpt4 book ai didi

java - 流关闭 IO 异常 Java

转载 作者:行者123 更新时间:2023-11-29 04:38:04 25 4
gpt4 key购买 nike

我正在运行以下程序并收到流关闭 IO 错误。但仅在第二个循环中。第一个工作正常。谁能告诉我为什么? (我检查过该文件存在且不为空。)

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

这是错误信息:

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at com.protonmail.taylor.faebl.development.main.perform(main.java:72)
at com.protonmail.taylor.faebl.development.main.main(main.java:23)

非常感谢您的帮助:)

最佳答案

您显然不能同时激活两个检索流,这不足为奇。只需重新排序您的代码:

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

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

当然,您随后会发现,如果任一输入超过一个缓冲区,它就无法工作,但您没有问过这个问题。

关于java - 流关闭 IO 异常 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40390774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com