Java PrintWriter正在引发FileNotFoundException
我有一个方法:Java PrintWriter正在引发FileNotFoundException,java,filenotfoundexception,printwriter,Java,Filenotfoundexception,Printwriter,我有一个方法: try { PrintWriter writer = new PrintWriter(new File(getResource("save.txt").toString())); writer.println("level:" + level); writer.println("coins:" + coins); writer.close(); } catch (FileNotFoundException e) { e.printSta
try {
PrintWriter writer = new PrintWriter(new File(getResource("save.txt").toString()));
writer.println("level:" + level);
writer.println("coins:" + coins);
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
它抛出了这个错误:
java.io.FileNotFoundException: file:/Users/lpasfiel/Desktop/Java%20Games/Jumpo/out/production/Jumpo/com/salsagames/jumpo/save.txt (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
at java.io.PrintWriter.<init>(PrintWriter.java:263)
at com.salsagames.jumpo.Variables$Methods.save(Variables.java:49)
java.io.FileNotFoundException:file:/Users/lpasfiel/Desktop/java%20Games/Jumpo/out/production/Jumpo/com/salsagames/Jumpo/save.txt(无此类文件或目录)
位于java.io.FileOutputStream.open0(本机方法)
在java.io.FileOutputStream.open(FileOutputStream.java:270)
位于java.io.FileOutputStream。(FileOutputStream.java:213)
位于java.io.FileOutputStream。(FileOutputStream.java:162)
位于java.io.PrintWriter(PrintWriter.java:263)
位于com.salsagames.jumpo.Variables$Methods.save(Variables.java:49)
它说错误在
PrintWriter=…
行中,文件肯定存在。(但这不应该是个问题,对吗?)。这种方法在ImageIcon中的.png
s中有效,所以我不明白为什么会有任何不同。有人能解释一下为什么这不起作用以及如何修复它吗?让我们仔细看看这一行:
java.io.FileNotFoundException: file:/Users/lpasfiel/Desktop/Java%20Games/Jumpo/out/production/Jumpo/com/salsagames/jumpo/save.txt (No such file or directory)
如果查看FileNotFoundException
的其他示例,您会注意到一条典型的消息如下所示:
java.io.FileNotFoundException: /some/path/to/file.txt (No such file or directory)
或
简而言之,典型的“未找到文件”消息以绝对或相对文件路径名开头。但在您的示例中,消息显示一个“file:”URL
我认为这就是问题所在。我认为您已经使用URL字符串而不是路径名创建了一个文件
。文件
构造函数不检查此1,但当您尝试实例化FileWriter时,操作系统会抱怨找不到具有该路径名的文件
(线索是假定的路径名以“file:”开头,并且它还包括一个转义的空格。)
解决方案:
类似于下面的一个。。。取决于getResource()
返回的内容
File file = new File(getResource("save.txt").toURI());
PrintWriter writer = new PrintWriter(file);
或
1-不应该。URL字符串实际上是语法上有效的路径名。由于允许
文件
表示文件系统中不存在的文件路径,因此文件
构造函数没有理由拒绝URL字符串。根据请求,这是有效的:
try {
PrintWriter writer = new PrintWriter(new File(getResource("save.txt").toURI()));
writer.println("level:" + level);
writer.println("coins:" + coins);
writer.close();
} catch (FileNotFoundException | URISyntaxException e) {
e.printStackTrace();
}
您是否尝试将完整路径传递给
文件对象?该文件在类路径中吗?它与类位于同一目录中,但我将尝试完整路径。不,不起作用。您通过了哪个路径?对于完整路径,我的意思是类似于C:/users/userX/../save.txt
。根据您的情况调整此选项,然后重试
PrintWriter writer = new PrintWriter(getResource("save.txt").openStream());
try {
PrintWriter writer = new PrintWriter(new File(getResource("save.txt").toURI()));
writer.println("level:" + level);
writer.println("coins:" + coins);
writer.close();
} catch (FileNotFoundException | URISyntaxException e) {
e.printStackTrace();
}