【发布时间】:2018-04-19 11:41:59
【问题描述】:
我有一个方法:
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)
它说错误在PrintWriter writer = ...的行中,该文件肯定存在。 (但这应该不是问题,不是吗?)。此方法适用于 ImageIcon 中的 .pngs,所以我不明白为什么会有任何不同。有人可以解释为什么这不起作用以及如何解决它吗?
【问题讨论】:
-
您是否尝试将完整路径传递给
File对象?该文件在类路径中吗? -
和类在同一个目录下,不过我试试全路径。
-
不,没用。
-
你经过了哪条路?
-
完整路径我的意思是
C:/users/userX/.../save.txt。根据您的情况进行调整,然后重试
标签: java filenotfoundexception printwriter