Salsa Gal
Salsa Gal

Reputation: 45

PrintWriter is throwing FileNotFoundException

I have a method:

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();
}

And it throws this error:

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)

It says the error is in the line with PrintWriter writer = ... The file definitely exists. (but that shouldn't be a problem, should it?). This method has worked for .pngs in an ImageIcon, so I don't see why it would be any different. Could someone explain why this doesn't work and how to fix it?

Upvotes: 1

Views: 904

Answers (2)

Stephen C
Stephen C

Reputation: 718788

Lets look carefully at this line:

java.io.FileNotFoundException: file:/Users/lpasfiel/Desktop/Java%20Games/Jumpo/out/production/Jumpo/com/salsagames/jumpo/save.txt (No such file or directory)

If you look at other examples of FileNotFoundException, you will notice that a typical message looks like this:

java.io.FileNotFoundException: /some/path/to/file.txt (No such file or directory)

or

java.io.FileNotFoundException: dir/file.txt (No such file or directory)

In short, a typical "file not found" message starts with an absolute or relative file pathname. But in your example, the message shows a "file:" URL.

I think that that is the problem. I think that you have created a File using a URL string rather than a pathname. The File constructor doesn't check this1, but when you attempt to instantiate the FileWriter, the OS complains that it cannot find a file with that pathname.

(The clues are that the supposed pathname starts with "file:", and that it also includes a %-escaped space.)

Solution:

Something like one of the following ... depending on what getResource() is returning.

  File file = new File(getResource("save.txt").toURI());
  PrintWriter writer = new PrintWriter(file);

or

  PrintWriter writer = new PrintWriter(getResource("save.txt").openStream());

1 - And it shouldn't. A URL string is actually a syntactically valid pathname. Since a File is allowed to represent a file path that doesn't exist in the file system, there would be no basis for the File constructor to reject a URL string.

Upvotes: 2

SeverityOne
SeverityOne

Reputation: 2691

As requested, this worked:

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();
}

Upvotes: 1

Related Questions