Platform Independent Paths in Java

Platform independent paths in Java

The File class contains the following public members that you can use for platform specific file paths:

static String pathSeparator:

The system-dependent path-separator character, represented as a string for convenience.

static char pathSeparatorChar:

The system-dependent path-separator character.

static String separator:

The system-dependent default name-separator character, represented as a string for convenience.
static char separatorChar:

The system-dependent default name-separator character.

File path issue on windows vs Unix in java

Use FileSystem.getSeparator() or System.getProperty("file.separator") instead of using slashes.

EDIT:
You can get an instance of FileSystem via FileSystems.getDefault (JDK 1.7+)

Platform-independent way to get a path to store program data

The system property user.home should be pretty standard across most desktop systems.

System.out.println(System.getProperty("user.home"));

Note that this is the user the Java process runs under - so in case of server-side Java process, you would need to store information for the users of your app in your own data structure, as your app's users are not known to the OS.

Regarding a system-wide storage location, you may need to detect the OS version and compute the path. Another problem is that you would most likely need to escalate privileges to write to a system-wide location.

Fix Hardcoded Windows Paths in Java so that it works in linux also

Instead of hardcoding separator \ or /, use File.separator

String filePath = directoryPath + File.separator + "report.html";

or event better do not use path literals at all but create Path object like

Path filePath = Paths.get(directoryPath, "report.html");

such Path object you can pass to the File constructor/utils but also you can always take String value of the path by calling toString() on the instance

Difference between File.separator and slash in paths

With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.

You might want to use File.separator in UI, however, because it's best to show people what will make sense in their OS, rather than what makes sense to Java.

Update: I have not been able, in five minutes of searching, to find the "you can always use a slash" behavior documented. Now, I'm sure I've seen it documented, but in the absense of finding an official reference (because my memory isn't perfect), I'd stick with using File.separator because you know that will work.

reading a file using File separator in java

File a = new File(File.separator + "com" + File.separator + "test.txt");
System.out.println(a.getAbsolutePath());

Will likely output something that you were not expecting.

If you post what that returns, I can tell you what your problem was/is.

It likely uses the project directory, which you can output using:

how to Locate the path of the Current project in Java, Eclipse?


Solution:

If the path on windows is:

C:\com\test.txt

But on linux you want:

/com/test.txt

You want to use:

new File(System.getenv("SystemDrive") + File.separator + "com" + File.separator + "test.txt")

As this will function as described above.

How specify platform independent paths in java.security and policy files?

If it were me, I would accept the fact that I needed different files and generate the linux version from the windows version by having a script to remove the "/".

However if you really need to use the same file, you could swap one pain for another by just defining another environment variable to represent the "/" on windows and have it blank on linux.

# our.hack = "/" on windows
# our.hack = "" on linux
policy.url.3=file:${our.hack}${our.home}/lib/OurSecurity.policy

Perhaps our.hack is not the best name in a production environment, but you get the idea. The variable is not used for anything else, so should not interfere with the rest of the app.



Related Topics



Leave a reply



Submit