我有一个方法:
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 = ...,文件肯定存在。(但这不应该是个问题,对吗?)这个方法已经在.png的ImageIcon中起作用了,所以我不知道为什么会有什么不同。有人能解释一下为什么这不起作用吗?如何解决?
发布于 2018-04-19 12:03:35
根据要求,这起了作用:
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();
}https://stackoverflow.com/questions/49920237
复制相似问题