簡體   English   中英

使用多個掃描儀時找不到文件

[英]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) { } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM