java - Why do I get ProviderMismatchException when I try to .relativize() a Path against another Path? -


[note: self answered question]

i have opened filesystem zip file using java.nio. have gotten path filesystem:

final path zippath = zipfs.getpath("path/into/zip"); 

now have directory on local filesystem have obtained using:

final path localdir = paths.get("/local/dir") 

i want test whether /local/dir/path/into/zip exists, check existence using:

files.exists(localdir.resolve(zippath)) 

but providermismatchexception. why? how fix this?

this behaviour documented, albeit not visible. have delve java.nio.file package description see, right @ end, that:

unless otherwise noted, invoking method of class or interface in package created 1 provider parameter object created provider, throw providermismatchexception.

the reasons behaviour may not obvious, consider instance 2 filesystems can define different separator.

there no method in jdk there. if filesystems use same separator can work around using:

path1.resolve(path2.tostring()) 

otherwise utility method can help:

public static path pathtransform(final filesystem fs, final path path) {     path ret = fs.getpath(path.isabsolute() ? fs.getseparator() : "");     (final path component: path)         ret = ret.resolve(component.getfilename().tostring());     return ret; } 

then above can written as:

final path localpath = pathtransform(localdir.getfilesystem(), zippath); files.exists(localdir.resolve(localpath)); 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -