0

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!!

2 Answers 2

2

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.)

0

You are mixing two things here. You are saving to an absolute path '/files/highscores.txt', but you are reading from the classpath, instead of the file system path. You'd have to choose one approach, but I'd stay away from absolute paths here.

However, a better approach might be to use the Preferences API, or Properties.load / store here. And if you're going to do IO operations, it might be better to look into Apache Commons IO, in which reading a file can be as trivial as doing FileUtils.readLines(file, "UTF-8");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.