Ian
Ian

Reputation: 1707

Processing unix paths in Windows

What's the best way of processing *ix file path strings when running on Windows?

If I just use Paths.get() it invokes Filesystem.getDefault() which ends up processing it like a Windows path. The parsing seems to work in my tests but they're pretty rudimentary, and of course toString uses the wrong path separator.

Can I manually load up the LinuxFileSystem somehow? Or should I use the commons-io parser instead?

Upvotes: 0

Views: 230

Answers (2)

Little Santi
Little Santi

Reputation: 8783

Can I manually load up the LinuxFileSystem somehow?

I doubt it, because one JDK distribution is oriented either to Unix or either to Windows. The only chance I think of is that you look for an open source distribution of some UnixFileSystemProvider and import into your application's runtime.

But even in that case, I have my doubts it will work 100%, because a FileSystemProvider class deals with low-level details from the underlying OS - not just path separators, so there is a risk that it won't be compatible over a Windows filesystem.

Upvotes: 1

Ian
Ian

Reputation: 1707

Looks like @Little Santi's comment on the question is in fact the answer. If I run:

for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
  System.out.println(provider.getClass().getName());
}

I get:

sun.nio.fs.WindowsFileSystemProvider
com.sun.nio.zipfs.ZipFileSystemProvider

If I interpret this correctly it means I can't use the LinuxFileSystem path methods under Windows but should use commons-io instead.

Upvotes: 0

Related Questions