Java PrintWriter Save File to Source Package in NetBenas

I am using the following java code in NetBeans to read and write to a file.

import java.io.*;
import java.util.*;

public class FileReadWrite {

    StringTokenizer tokenizer;
    BufferedReader inFile;
    PrintWriter outFile;
    File myFile;
    private int[] highscores = new int[3];

    public FileReadWrite() throws IOException, FileNotFoundException {

    }

    public int[] ReadFromSave() throws IOException, FileNotFoundException {

        inFile = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/files/highscores.txt" )));
        tokenizer = new StringTokenizer(inFile.readLine());
        int i = 0;
        while (tokenizer.hasMoreTokens()) {
            highscores[i] = Integer.parseInt(tokenizer.nextToken());
            i++;
        }

        inFile.close();
        return highscores;

    }

    public void SaveFile(int score) throws IOException, FileNotFoundException {

        if (score >= highscores[0]){
            highscores[2] = highscores[1];
            highscores[1] = highscores[0];
            highscores[0] = score;
        }
        else if (score >= highscores[1]){
            highscores[2] = highscores[1];
            highscores[1] = score;
        }
        else if (score >= highscores[2]){
            highscores[2] = score;
        }

        PrintWriter outFile = new PrintWriter(new FileWriter("/files/highscores.txt"));

        String line = highscores[0] + " " + highscores[1] + " " + highscores[2];

        outFile.println(line);
        outFile.close();

    }

}

I have coded a tetris program and want to save the highscores after each game. And when the user opens up the app next time they are shown the top 3 scores. The ReadFromSave() method started to work fine after I aded "getClass().getResourceAsStream". However I can't get the SaveFile() method working. I gives error "path/file not found". How should I use the PrintWriter in NetBeans so that it saves into the source packages and thus updating the jar. So when the app is fully closed and reopened it has the recent scores printed in. If this is not possible where can I save data so its not lost?

Thank you!!

Jon Skeet
people
quotationmark

If you want to be able to save the scores again, using getResourceAsStream isn't really a good idea - that's meant for resources which are bundled with the application, often within a jar file, and often read-only.

You might want to consider using the Java preferences API or work out some specific location for the file to use to store the high scores. Then just write to that file when saving, and read from it while loading. I wouldn't personally use PrintWriter (it swallows exceptions) or FileWriter (you can't specify the character encoding), but both should work. You just need to make sure you load the scores from the same file as you save it to. (It's not clear whether /files/highscores.txt is really an appropriate file to save to... do you have a /files directory? Were you expecting this to be relative to your application's working directory?)

(Also, it's a good idea to start following Java naming conventions, using camelCasing for method names... and use a try-with-resources statement to close writers, streams etc when you're finished with them, instead of manually calling close.)

people

See more on this question at Stackoverflow