首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PrintWriter扔FileNotFoundException

PrintWriter扔FileNotFoundException
EN

Stack Overflow用户
提问于 2018-04-19 11:41:59
回答 1查看 753关注 0票数 1

我有一个方法:

代码语言:javascript
复制
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();
}

它抛出这个错误:

代码语言:javascript
复制
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中起作用了,所以我不知道为什么会有什么不同。有人能解释一下为什么这不起作用吗?如何解决?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-19 12:03:35

根据要求,这起了作用:

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49920237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档