1

I'd like to save data (just String that could be stored in a small text file) and load it automatically to the app when it is started. I imagine a text file in the program installed directory but I dont know if it is possible. How could the program know the path of the directory where it is installed? Or should I just ask for the first time the user to tell the program the location and somehow the program save this location info?

And it should be work on Windows, Mac and Linux.

lipilocid
  • 87
  • 8

1 Answers1

1

I generally use System.getProperty("user.home") to get the default home directory on different platforms. One easy and reliable way would be to use something like:

String fileLocation = new StringBuilder(System.getProperty("user.home"))
                                          .append(File.separator)
                                          .append("myApplicationDir")
                                          .append(File.separator)
                                          .append("fileName.txt")
                                          .toString();

As for

....and load it automatically to the app when it is started

I would suggest suggest to store a file with meta data stored relative to the JAR. Or you can just keep a utility class with static constant stored in order to find out what the application stored directory is and then use it to retrieve it when the constructor of any object is invoked or on any callback.

Shababb Karim
  • 3,614
  • 1
  • 22
  • 35