使用多个扫描仪时找不到文件
File not found when using multiple scanners
我正在 Eclipse 中编写一个简单的程序来输入和比较两个文本文件。但是,我似乎无法同时导入这两个文本文件。如果我删除了一个新的扫描仪对象,它会清除我的另一个文件;否则,它会给我一个错误,即在两者上都找不到该文件。这两个文件都在我的源文件夹中的同一个位置。代码如下:
textfile1 = new File("Text1.txt");
Scanner text1 = new Scanner(textfile1);
textfile2 = new File("Text2.txt");
Scanner text2 = new Scanner(textfile2);
提前致谢!
用 try-catch 块试试这段代码,让我知道它是否有效。
// 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) {
}
我正在 Eclipse 中编写一个简单的程序来输入和比较两个文本文件。但是,我似乎无法同时导入这两个文本文件。如果我删除了一个新的扫描仪对象,它会清除我的另一个文件;否则,它会给我一个错误,即在两者上都找不到该文件。这两个文件都在我的源文件夹中的同一个位置。代码如下:
textfile1 = new File("Text1.txt");
Scanner text1 = new Scanner(textfile1);
textfile2 = new File("Text2.txt");
Scanner text2 = new Scanner(textfile2);
提前致谢!
用 try-catch 块试试这段代码,让我知道它是否有效。
// 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) {
}