Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What is the best way to find a path relative to the folder where a java application is "installed"?

I have a class with a static method: public static void saveToFile(String fileName)

When I call it with an absolute path, it works, but what I really want is the relative path to where the application is run from, and a folder.

I have not deployed my application, but right now I want to find a path relative to the (Netbeans) project root, and a folder within called data: ProjectNamedatafile.dat. Should I use the File class or make it into a URI or something?

Note that I prefer it to be system-independent and will still work if the application is deployed. Eventually the (relative) pathname will be stored in a properties file.

Sorry if this question is a duplicate, any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
791 views
Welcome To Ask or Share your Answers For Others

1 Answer

What is the best way to find a path relative to the folder where a java application is "installed"?

OS manufacturers have been saying for a long time not to save files in the application directory.

Note that I prefer it to be system-independent and will still work if the application is deployed.

Instead put the File in a sub-directory of user.home. User home is where it should be possible to establish a file object that can be read or written. It is also a place that is reproducible across runs, and platform independent.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...