Saving files to appropriate location

I currently can save my game files just fine, but I’ve been saving to the assets folder. This is a universal directory that works for any installation of the game. However, I need to be writing to a better location because /assets ends up being in users’ Program Files, and that requires administrator privileges to save to and is not what that location was designed for.

For distributed installations of my game, what is the appropriate way to direct game saves to a better file location?

Currently, it is:
[icode]BufferedWriter out = new BufferedWriter(new FileWriter(“assets\WriteHere\GameSettings.txt”));[/icode]

I either need them to tell me or I need to know a better location. How do people recommend I go about getting the user’s desired save location, or is there a command I don’t know that lets me grab their Users folder or something?

This is what I’m using right now:

OSX/Linux:

// ~/{user}/Documents
String home = System.getProperty("user.home");
File documents = new File(home, "Documents");

Windows:


// %APPDATA (c:\Users\{user}\AppData\Roaming)
String appdata = System.getenv("APPDATA");
File roaming = new File(appdata);

From there I create {Company}/{Product} subfolders.

Not sure yet how portable that is. In any case you want to guard your save function and check for exceptions, then warn the user, retry, and/or use a fallback location. There are crazy configurations out there.

Nice test case, on Windows at least: check your save code with a user name containing spaces and special characters. This can break in so many ways it isn’t funny.