我正在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) {
}