我正在创建一个文件系统来浏览 jar,以防对我的资源的访问是在一个 jar 中。
然后我注意到,在创建一个新的 FileSystem 时,它实际上在使用 Paths NIO 类时注册为默认文件系统。
但 Filesystems.getDefaultSystem 不断返回硬盘常规。
为什么这种行为不一致且如此透明?当询问相对路径为 myResources/myResource.txt 时,如何询问 Paths 实际使用的文件系统?
System.out.println("Default FS: "+FileSystems.getDefault().getClass().getName());
URI rscURI = Test.class.getClassLoader().getResource("folder").toURI();
try{ Paths.get(clURI).getFileSystem(); }
catch(FileSystemNotFoundException e){
System.out.println("A new Filesystem for "+clURI.getScheme()+" scheme is created.");
FileSystems.newFileSystem(clURI, Collections.emptyMap());
System.out.println("Default FS: "+FileSystems.getDefault().getClass().getName());
}
return Paths.get(rscURI)