tags:

views:

35

answers:

3

Hi there. 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 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.

+1  A: 

You could use the user.home property to get the home directory of the current user:

System.getProperty("user.home")
ilikeorangutans
+1  A: 

How about using System.getProperty("os.name")? Then set the filepath according to the OS. Another way would be to pass in the root as a parameter.

Chuk Lee
+2  A: 

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.

ig0774
Yes, the project structure is the same in both systems, just changes the path to the project root directory. How do i load a file without mentioning the path to the root directory? Tried with "file.xxx" "/TheProject/res/file.xxx" and does not work.
Gabriel A. Zorrilla
The javadoc for File explains the conversion process in detail http://java.sun.com/javase/6/docs/api/java/io/File.html. Essentially, given the structure /TheProject/res/file.xxx or on Windows C:\TheProject\res\file and assuming that in both cases the app is launched from the TheProject directory, `new File("res/file.xxx");` should get you the correct file reference.
ig0774