简体   繁体   中英

distributing my java app, HOW do I manage directories

My java applications is almost complete and now I am giving it a finishing touch. Although certain code requires to read data from specific directories on the computer. On my machine I did it in the following way:

FileOutputStream fos=new FileOutputStream(C:/User/AnirudhVarma/Document/Appname/foldername/file,true);

But on the users machine how do I ensure that it creates it in the following directory?

C:/enduser/appname/folder/file

Hard coding folder structure in code is very bad idea.

One way to handle this is:

You need to have a properties file something like that where user need to enter path to required folder. Mention this as requirement in install docs.

In your code you need to make sure folder has required content before proceeding. If not valid folder, just show error message with proper information to setup the directory path.

System.getProperty("user.dir") will return C:/User/AnirudhVarma on you machine. From there, I'd suggest you need to consider the platform. Under windows, you should store this kind of information into the AppData folder.

So you would end up with something like System.getProperty("user.dir") + File.seperator + "AppData/Appname/foldername/file"

Under MacOS, I believe the convention is to use .AppName instead, something like System.getProperty("user.dir") + File.seperator + ".Appname/foldername/file"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM