Creating a local storage directory in Java desktop app
Asked Answered
D

5

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.
Dropkick answered 21/6, 2012 at 13:42 Comment(0)
B
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.

Battik answered 21/6, 2012 at 13:50 Comment(3)
That might be a good place for preferences as he says, but history data should probably not be there.Grammer
Indeed it doesn't seem to fit entirely, but interesting nonetheless. I didn't know of this API.Dropkick
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 orderBattik
N
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.
Nyaya answered 21/6, 2012 at 15:9 Comment(0)
S
0

Use user.home system property. Like this:

String userHomePath = System.getProperty("user.home");
File myAwesomeFolder = new File(useHomePath, "myAweSomeApp");
myAwesomeFolder.mkdirs();
Slinky answered 21/6, 2012 at 13:47 Comment(5)
I've tested that on Windows 7 where it returns C:\User\John, whereas I probably want C:\Users\John\AppData\LocalDropkick
@BartvanHeukelom, well, that's because it returns your home folder, not AppData.Slinky
user.home is a place that is guaranteed to be writable, and is x-plat. Maybe you can use an environment variable where known and user.home otherwise.Nyaya
@Slinky So it doesn't return what I need, does it? ;)Dropkick
@AndrewThompson Using the APPDATA environment var might work, yes. Why don't you put it as answer?Dropkick
L
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

Livvyy answered 21/6, 2012 at 13:53 Comment(2)
See question and npe's answerDropkick
Yeah, I noticed when I was finished dropping my answer.Livvyy
T
-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.

Tran answered 21/6, 2012 at 13:51 Comment(1)
This is anything but cross-platform.Dropkick

© 2022 - 2024 — McMap. All rights reserved.