简体   繁体   English

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

[英]File not found when using multiple scanners

I'm writing a simple program in Eclipse to input and compare two text files. 我正在Eclipse中编写一个简单的程序来输入和比较两个文本文件。 However, I can't seem to import the two text files at the same time. 但是,我似乎无法同时导入两个文本文件。 If I delete one of the new scanner objects, it clears my other file; 如果删除一个新的扫描仪对象,它将清除另一个文件; otherwise, it gives me an error that the file was not found on both. 否则,这会给我一个错误,即两者都找不到该文件。 Both files are in the same place in my source folder. 这两个文件都在我的源文件夹中的同一位置。 The code is as follows: 代码如下:

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

Thanks in advance! 提前致谢!

Try this code with the try-catch block and let me know if it worked or not. 尝试使用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) { } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM