gpt4 book ai didi

java - 扫描仪和多线程问题?

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

我有以下代码来读取整个文件数据:

calling method(String zipFile){
ZipInputStream zis =
new ZipInputStream(new FileInputStream(zipFile));
//get the zipped file list entry
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();

File newFile =
new File(Constants.OUTPUT_FOLDER + File.separator +
fileName);

if (ze.isDirectory()) {
new File(newFile.getParent()).mkdirs();
} else {
new File(newFile.getParent()).mkdirs();
createBlobDomain(zFile,ze);

}
}
ze = zis.getNextEntry();
}

zis.closeEntry();
zis.close();
}


public String method(ZipFile zf, ZipEntry ze){
scan = new Scanner(zf.getInputStream(ze));
if(scan.hasNext())
fullText = scan.useDelimiter("\\A").next();
return fullText;
}

请从编译的角度忽略它,因为我删除了一些与这里无关的代码。当作为单个实例从 webapp 运行时,它工作正常。但是我同时从两个不同的浏览器运行它然后我遇到了以下异常。请告知可能出了什么问题以及如何解决。

java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1347)

最佳答案

我认为 scan = new Scanner(zf.getInputStream(ze)); 这行造成了问题。我从你的代码中了解到,scan 是一个实例变量,你正在为每个线程分配一个 new Scanner。我建议在您的方法中将其作为局部变量。如果我误解了什么,请纠正我。

Scanner scan = new Scanner(zf.getInputStream(ze))

关于java - 扫描仪和多线程问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893411/

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