gpt4 book ai didi

java - 使用多个扫描仪时找不到文件

转载 作者:行者123 更新时间:2023-12-01 11:28:50 24 4
gpt4 key购买 nike

我正在 Eclipse 中编写一个简单的程序来输入和比较两个文本文件。但是,我似乎无法同时导入两个文本文件。如果我删除一个新的扫描仪对象,它会清除我的其他文件;否则,它会给我一个错误,表明在两者上都找不到该文件。这两个文件位于我的源文件夹中的同一位置。代码如下:

textfile1 = new File("Text1.txt");
Scanner text1 = new Scanner(textfile1);
textfile2 = new File("Text2.txt");
Scanner text2 = new Scanner(textfile2);

提前致谢!

最佳答案

使用 try-catch block 尝试此代码,并让我知道它是否有效。

// sometimes the compiler complains if there was a scanner opened without a try-catch block around it
try {
final File FILE1 = new File("text1.txt"); //it is always a good thing to make a file as final, it gives an easy reference for the reader
final File FILE2 = new File("text2.txt");
Scanner t1 = new Scanner(FILE1);
Scanner t2 = new Scanner(FILE2);
//after you are done close the scanner
t1.close();
t2.close();
} catch (FileNotFoundException ex) {
}

关于java - 使用多个扫描仪时找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30608822/

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