Question

In particular, I'd like to use the (unfortunately not visible) sun.nio.fs.Globs.toUnixRegexPattern(String glob).

Ok, stepping back and giving a bit of context

I have an iterator of pathes into a remote, unix-like file system (think ssh unixhost find path -type f). I also have a user-supplied glob pattern which I now want to match each path against.

On a unix machine, the following works just fine:

matcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);
// ...
for (String s : remoteFind(...)) {
    if (matcher.matches(Paths.get(s))) {
       // matches, do something
    }
}

But when this is run on Windows, the same program totally fails because the FileSystems.getDefault() returns a Windows filesystem (the horror, the horror) and '\' is used as separator, etc. You get the picture. Nothing matches.

Of course I can stop all this nonsense and just rewrite (or rather, copy) sun.nio.fs.Globs.toUnixRegexPattern(String glob), but is there another, more elegant way?

Was it helpful?

Solution

Ok, so just to close this question, as stated in the comments I ended up writing a method in my FileUtil that is almost verbatim a copy of sun.nio.fs.Globs.toUnixRegexPattern(String glob). Works great.

If somebody finds a better way please add a different answer here.

OTHER TIPS

If you do not make any file system operations locally, you could try to set

 -Dfile.separator=/

system variable to mimic the unix path separator. This variable should be passed to JVM on startup

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top