简体   繁体   中英

Where to typically save external data from Java application?

Ok, the question is quite simple.

I have a Java program, which I am extracting some save-files to an external location.

Right now, I use C:/ApplicationName , however, that is a very bad way to do that.

I know alot of locations i could use, for instance:

%Appdata%
C:\Program Files (x86)
C:\Users\Users\Documents (Ive seen some indie games use this.)

Other locations?

But I can't figure out, when to use the proper one.

And if i want to support Linux and OSX, is there a libary, which supports that, or do i manually have to wrap them into an if/else with System.getProperties("os.name") ?

Keep in mind that sometimes when games save to a specific directory, they aren't built to be cross-platform.

There are two ways you could do this. The first (and personally my preferred version) is to save in the directory of the application. This also has the advantage of being portable. However, the saves are linked to the application, so if the user deletes the application and reinstalls later they'll lose their data (which may or may not be a good thing).

Another option is to use System.getProperty() .

Specifically, you can use:

System.getProperty("user.dir");
System.getProperty("user.home");
System.getProperty("user.name");

To figure out where to put your files.

This will be a lot more complicated, so the first method is preferred.

If it's data the user does not need direct access to, such as stored application/game state data, then it's appropriate to store it in the user's application data directory. Try this:

1) Determine the OS.

2) Get the user home directory.

System.getProperty("user.home");

3) Append OS-specific application directory:

Mac: /Library/Application Support/MyApp

Windows: \\\\Application Data\\\\MyApp

Linux: MyApp (there's no convention here that I know of)

If the data needs to be exported, such as saving a document from your application, then ask the user where to store the file via a file dialog, defaulting to their Documents directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM