我已经研究过如何使用扫描仪方法(因为我是新手)并且我的代码看起来与其他代码完全相同,但仍然找不到文件。我的.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();
}
}
});
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
从文件创建扫描程序实例(使用new File("StationInfo.txt")
)时,默认文件位置位于您从中调用命令的文件夹中。尝试将txt
文件移动到运行代码的位置,或提供文件的完整路径(例如new File("C:\Users\...\...\StationInfo.txt")
)。
答案 2 :(得分:0)
虽然有很多方法可以在应用程序中查找,获取和加载资源,但是如果你想获取保存在src文件夹中的文件,那么你可以尝试下面的代码。
File f = new File("src/StationInfo.txt");