Paths.get - specify a platform?

Viewed 162

I am working on an application that is being developed on both Mac and Windows. We are generating some file paths and want the paths generated to all be in the UNIX flavor with forward slashes. Calling Paths.get(...) on the Windows machine produces paths with the backslashes, but I want it to produce paths with forward slashes.

I couldn't find documentation about "setting" or specifying the path separator to be used.

1 Answers

I tried to find it some time ago too but it's impossible. It's static method in Paths.get(...)

java.nio.file.FileSystems#getDefault

which returns the static final field

static final FileSystem defaultFileSystem = defaultFileSystem();

and java.nio.file.FileSystems.DefaultFileSystemHolder#defaultFileSystem detects your filesystem.

But in the instance of FileSystem the type of slash is hardcoded: https://github.com/frohoff/jdk8u-jdk/blob/master/src/windows/classes/sun/nio/fs/WindowsFileSystem.java#L249

So i think the simplest way for you is to use something like https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html

Related