FIleNotFoundException Java-can';找不到txt文件
我正在尝试将一个.txt文件读取到eclipse控制台,但我似乎无法找出问题所在,当我想读取它时,我总是会收到一个FIleNotFoundException Java-can';找不到txt文件,java,filenotfoundexception,Java,Filenotfoundexception,我正在尝试将一个.txt文件读取到eclipse控制台,但我似乎无法找出问题所在,当我想读取它时,我总是会收到一个FileNotFoundException——以下是我目前的代码: public static void main(String[] args) throws IOException{ Map<String,String> map = new HashMap<String, String>(); BufferedReader in; i
FileNotFoundException
——以下是我目前的代码:
public static void main(String[] args) throws IOException{
Map<String,String> map = new HashMap<String, String>();
BufferedReader in;
in = new BufferedReader(new FileReader("./morse-code.txt"));
String line = "";
while ((line = in.readLine()) != null ){
String parts[] = line.split("\t");
map.put(parts[0], parts[1]);
}
in.close();
System.out.println(map.toString());
}
但是,如果我想从文件中读取,我仍然会遇到以下错误:
Exception in thread "main" java.io.FileNotFoundException: C:\Users\User\workspace\AuD\morse-code.txt (Das System kann die angegebene Datei nicht finden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at orkgrunzer.OrkGrunzer.main(OrkGrunzer.java:16)
线程“main”java.io.FileNotFoundException中的异常:C:\Users\User\workspace\AuD\morse-code.txt
位于java.io.FileInputStream.open0(本机方法)
位于java.io.FileInputStream.open(未知源代码)
位于java.io.FileInputStream。(未知源)
位于java.io.FileInputStream。(未知源)
位于java.io.FileReader。(未知源)
位于orkgrunzer.orkgrunzer.main(orkgrunzer.java:16)
我找不到问题,也许有人可以帮我:)为了保护自己不受文件系统的影响,您可以将文件放在类路径的根目录下,并按以下方式加载:
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(Class.class.getResourceAsStream("/morse-code.txt")));
如果您决定将其放在某些文件夹下,例如:
resources/files/codes/morse-code.txt
并将文件夹resources
放在类路径上,然后您必须通过以下方式引用它:
"/files/codes/morse-code.txt"
是否定义了任何包?如果在命令提示符下执行
notepad C:\Users\User\workspace\AuD\morse code.txt
,该文件是否打开?您在此处读取用户目录:System.getProperty(“User.dir”)代码>不是当前工作目录尝试使用绝对路径而不是相对路径。
"/files/codes/morse-code.txt"