Load/save text file where application is.

D

daniel.w.gelder

Hi, I have a .jar that runs on various computers...sometimes Linux,
sometimes Windows, etc. Can I get the path of the directory it's
currently in so I can make a File() with it to save and load data? It's
a simple private storage file and shouldn't have to open a FileChooser.

Thanks
Dan
 
A

Abhijat Vatsyayan

Hi, I have a .jar that runs on various computers...sometimes Linux,
sometimes Windows, etc. Can I get the path of the directory it's
currently in so I can make a File() with it to save and load data? It's
a simple private storage file and shouldn't have to open a FileChooser.

Thanks
Dan
You can use the system property "user.dir" to get the working directory.
You can also try using "." for creating the new File but I am not sure
how portable that is.

Try the following code (I have not tested it!) -

String workDirName = System.getProperty("user.dir");
File workingDir = new File(workDirName);

Notice that it is quite possible that the working directory is NOT
writable. You can also try using the other system property "user.home"
to find a directory which is usually writable.

Abhijat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top