How to read/write .dat file within Jar file?

Okay this maybe very similar to a few questions out there and I've been looking all over, but can't manage to fix this. This is the only thing preventing my .jar from compiling properly so I would love to get this fixed.

I have a highScores.dat file that I need to read and write on within the .jar, but I can't get the Input and Output Streams to accept a path, only a filename, which doesn't make sense to me.

Here's the method's I'm using:

public class HighScoreManager {
  private File fileName = new File("highScores.dat");

public void addScore(String playerName, int score) {
    loadScoreFile();
    scores.add(new Score(playerName, score));
    updateScoreFile();
}

public void loadScoreFile() {
    try {
        inputStream = new ObjectInputStream(new FileInputStream(fileName));
        scores = (ArrayList<Score>) inputStream.readObject();
    } catch (FileNotFoundException e) {
        System.out.println("File not found: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("IO Error: " + e.getMessage());
    } catch (ClassNotFoundException e) {
        System.out.println("Class not found: " + e.getMessage());
    } finally {
        try {
            if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
        } catch (IOException e) {
            System.out.println("IO Error: " + e.getMessage());
        }
    }
}

public void updateScoreFile() {
    try {
        outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
        outputStream.writeObject(scores);
    } catch (FileNotFoundException e) {
        System.out.println("File not found: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("IO Error: " + e.getMessage());
    } finally {
        try {
            if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
        } catch (IOException e) {
            System.out.println("Update Error: " + e.getMessage());
        }}}}

The path is /highScores/highScores.dat which should be correct since images that are located in /images/ work fine with my buffered images within the .jar

Edit: Changing the filename to a path doesn't work. I've also tried getClass().getResourceAsStream to no avail.

Jon Skeet
people
quotationmark

You can get an InputStream using Class.getResourceAsStream (or ClassLoader) using a class or ClassLoader that "knows" about that jar file.

If you're finding that Class.getResourceAsStream() isn't working for you, you're probably passing in the wrong resource name. It's case-sensitive even on Windows, and relative to the class you call it on unless you use a leading /.

However, you can't easily write to a jar file in that way - you can build a complete new jar file using JarOutputStream, but I suspect you don't want to do that. I'd use a separate file, personally - you can always see if that exists and read from the jar file otherwise.

people

See more on this question at Stackoverflow