0

I have a textual-game that saves the state of an object using the serializable class, the object is stored in this way:

FileOutputStream fileOut = new 
FileOutputStream("./lib/savedGame/savedGame.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(obj);
        out.close();
        fileOut.close();
        System.out.printf("Serialized data is saved in ./lib/savedGame/savedGame.ser");
    } catch (IOException i) {
        i.printStackTrace();
    }
}

the problem is that I tried to create the jar file and then put it into a folder then create the folder ./lib/savedGame/ inside and in MacOsx works fine, when i try to execute the jar on windows it does not save anything. The same thing happens if i try to place a serialized object in a folder and deserialize it. works for macOs but not for windows. I think windows maybe executes the file from a different folder so it does not find the serializable object?

  • You cannot rely on current working directory being set. – Thorbjørn Ravn Andersen Oct 02 '17 at 21:53
  • MacOS and Windows are both particular about where things are placed and where you can write to. Depending on where the code was executed from and under what privilege level, you may find the OS silently prevents you from writing to certain locations. You should use well known locations, like `System.getProperty("user.dir") + File.seperator + "AppData/{your app name}/lib/savedGame/savedGame.ser"` on windows and `System.getProperty("user.home") + "/Library/Application Support/{your app name}/lib/savedGame/savedGame.ser")` on MacOS - then you remove a lot of the variability – MadProgrammer Oct 02 '17 at 21:54
  • You should also make sure the path exists, I use a combination of `File#exists` and `File#mkdirs` to ensure the directory structure exists before attempting to perform write operations – MadProgrammer Oct 02 '17 at 21:56
  • could you write an example to make me understand better? – Giacomo Benso Oct 02 '17 at 21:59
  • See https://stackoverflow.com/questions/35388882/find-place-for-dedicated-application-folder. – VGR Oct 02 '17 at 22:14

0 Answers0