6

I'm building a Java desktop application and need to store some local data (preferences and history). For this I'd like to create a new folder in the appropriate location, like AppData\myapp in Windows and ~/.myapp in Linux (and wherever is expected on a Mac).

What is the nice, cross-platform way to do that?


I've seen several questions on this site that ask about this, but either:

  • The asker wants to find Windows' Application Data (not cross-platform)
  • The solution is to create a folder in user.home (Linux style, not cross-platform) This is what I currently do, but I'm looking for an improvement.
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

5 Answers5

4

You could always use the Java Preferences API which will store info per-user and you don't have to worry about the implementation. Different implementations are available for different platforms but that's hidden from you (the client).

An alternative is to use the Apache Commons Configuration API, which is more complex, but gives you a lot more features.

Greg Chabala
  • 1,125
  • 2
  • 23
  • 35
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • That might be a good place for preferences as he says, but history data should probably not be there. – maba Jun 21 '12 at 13:51
  • Indeed it doesn't seem to fit entirely, but interesting nonetheless. I didn't know of this API. – Bart van Heukelom Jun 21 '12 at 13:53
  • I guess the question around history may depend on how *much* history. Both APIs will have the capability, but perhaps some load testing is in order – Brian Agnew Jun 21 '12 at 13:56
1
import java.io.File;

public class AppPathFolder {

    public static void main(String[] args) {
        String path = null;
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.indexOf("windows")>-1) {
            path = System.getenv("APPDATA");
        } else if (osName.indexOf("mac")>-1) {
            // get the env. variable for Mac..
            path = System.getenv("?");
            // etc. for Linux, Unix, Solaris..
        } else { //anything else
            path = System.getProperty("user.home");
        }
        File rootOfPath = new File(path);
        // create a sub-directory based on package name of main class..
        // perhaps prefixed with with java/appdata
        System.out.println(rootOfPath);
    }
}

Of course, there are other options for small amounts of data:

  • Apps. that are trusted or have no security manager might use the Preferences API
  • A desktop app. launched using Java Web Start has access to the JNLP API, wich offers the PersistenceService - available even to sand-boxed apps.
  • An applet can store cookies.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Use user.home system property. Like this:

String userHomePath = System.getProperty("user.home");
File myAwesomeFolder = new File(useHomePath, "myAweSomeApp");
myAwesomeFolder.mkdirs();
npe
  • 15,395
  • 1
  • 56
  • 55
0

How about storing the app-folder into the user home directory? You can get it per System.getProperty("user.home"), see http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

jayeff
  • 148
  • 9
-1

not confirmed about Linux but you can make entry to windows registry too.

either way,

String path = System.getProperty("user.home")+ "\\AppLication Data"+"\\xyzFolder";

this works for both windows 7 to because %appData% shows the path which legacy for above.

RTA
  • 1,251
  • 2
  • 14
  • 33