[英]Scanner not finding file
我研究了如何使用掃描儀方法(因為這是我的新手),我的代碼看上去與其他代碼完全相同,但仍然找不到文件。 我的.txt文件位於src文件夾中。
它引發以下錯誤:
java.io.FileNotFoundException: StationInfo.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at traingui.code.TrainGui$1.run(TrainGui.java:37)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
這是實現掃描程序的代碼:
package gui.code;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
public class Gui {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
//running the gui
public void run() {
makeAndShowGUI();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
從文件創建掃描儀實例時(使用new File("StationInfo.txt")
),默認文件位置在您從中調用命令的文件夾內。 嘗試將txt
文件移動到運行代碼的位置,或提供文件的完整路徑(例如new File("C:\\Users\\...\\...\\StationInfo.txt")
)。
盡管有很多方法可以在應用程序中查找,獲取和加載資源,但是如果您希望獲取保存在src文件夾中的文件,則可以嘗試以下代碼。
File f = new File("src/StationInfo.txt");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.