1

I know how to create a new folder if it is not already present in Java.

File directory = new File("path");
directory.mkdir();

Do all developers use the Program Files location to create folders for their apps to store data? Is that path always the same for every user who uses the app? How do Java developers adjust for the difference in Mac users file paths?

I'm also wondering if System.getProperty("user.home"); is the common way of navigating a system to create file paths?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • See this question: http://stackoverflow.com/questions/6561172/find-directory-for-application-data-on-linux-and-macintosh - it's a bit messy I'm afraid. – not all wrong Apr 10 '13 at 21:09
  • 1
    Be carefully calling `mkdir()` -- you have to pay attention to the return value. I prefer the newer `Files.createDirectory()` in Java 7. It ensures that the directory gets created. – Nathaniel Waisbrot Apr 10 '13 at 22:10
  • Why do you want to create a folder in Program Files? Are you making an installer or do you want to store application data? If you want to store application data the Program Files folder is discouraged.Use Application Data instead (AppData) . – Menelaos Apr 10 '13 at 22:15
  • *"How to create a folder in a Users Program File directory?"* Can that be rephrased as 'How to store user preferences?'? If so, launch the app. with JWS and use the [`PersistenceService`](http://pscode.org/jws/api.html#ps) to store the details. – Andrew Thompson Apr 10 '13 at 22:18

2 Answers2

0

For a 32-bit program on a 32-bit pc, or a 64-bit program on a 64-bit pc:

System.getenv("ProgramFiles")    

For a 32-bit program on a 64-bit pc:

 System.getenv("ProgramFiles(X86)")

These only work on Windows systems, obviously..

drew moore
  • 31,565
  • 17
  • 75
  • 112
0

For the few stand alone and java web start apps that save data to local I dont use program files. hate it as it has spaces and windows can add or remove permissions over versions.

instead I do this (And I tell the users about it so its not a surprise for one app I even open the folder as there is some user editable prefs.): 1. check if c:/apps/sel2in/noteMaker is there if not try to create 2. if error make d:/apps/.... 3. else use and make one using System.getProperty("user.home")

Use mkdirs so it makes the parent folders as needed.

fyi / works great in java in windows, java changes it internally and its easier to type than \ for me

For unix i usually take param 1 as a path to a properties file and that has the folder to save other files and other preferences.

tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • Awesome response! I appreciate the tip with /. I'm pretty new to Java. I think i'll take your approach with the if/else creation statements resorting to user.home if all else fails. – user2267769 Apr 10 '13 at 21:59
  • need to check error carefully if folder does not exist etc or c:/ does not exist or does not have write permission. better to call function around try catch and in finally use a default if path null or cant read (fileObject.canRead()) mark correct if it works – tgkprog Apr 10 '13 at 22:33