I'm doing some JAVA coding at home and at work. At home i have Linux, work, Windows. The rootpath to X file in Windows is c:\Documents And Settings\User\My Documents\Dropbox\file.xxx and in Linux is something like /media/My Documents/Dropbox/file.xxx
So, every time 开发者_Python百科i edit in either system, i have to manually change the root of the file in a new File(FILEPATH) statement. Is there a workaround for this? I bet if the file root is relative to the project resource tree would do the trick, but that's an Eclipse based solution, not JAVA, i believe.
How about using System.getProperty("os.name")
? Then set the file path according to the OS. Another way would be to pass in the root as a parameter.
A couple of suggestions:
A file in a subdirectory of the project is cross-platform portable (assuming, were you to launch the program outside of Eclipse you would maintain the file in the same location).
Store the file in a similar relative path to your home directory (~ on Linux %USERPROFILE% on Windows) and use
System.getProperty("user.home")
Store the file on the class path and use
ClassLoader.getResourceAsStream()
or similar.
You could use the user.home
property to get the home directory of the current user:
System.getProperty("user.home")
精彩评论