首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FileSystems.newFileSystem时出错( uri uri、Map<String、?> env)

使用FileSystems.newFileSystem时出错( uri uri、Map<String、?> env)
EN

Stack Overflow用户
提问于 2017-04-20 21:20:18
回答 1查看 4.1K关注 0票数 4

我使用以下代码:

代码语言:javascript
复制
private static FileSystem createZipFileSystem(String zipFileName, boolean create) throws IOException {
    final Path path = Paths.get(zipFileName);
    final URI uri = URI.create("file:" + path.toUri().getPath());
    final Map<String,String> env = new HashMap<String,String>();

    if(create) {
        env.put("create", "true");
    }

    return FileSystems.newFileSystem(uri, env);
}

当我调用它时(dest是我项目中的一个文件夹):

代码语言:javascript
复制
createZipFileSystem("dest", true);

我得到以下错误:

代码语言:javascript
复制
Exception in thread "main" java.lang.IllegalArgumentException: Path component should be '/'
at sun.nio.fs.UnixFileSystemProvider.checkUri(UnixFileSystemProvider.java:77)
at sun.nio.fs.UnixFileSystemProvider.newFileSystem(UnixFileSystemProvider.java:86)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:326)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)
at com.jerney.ziptest.utils.ZipNIO.createZipFileSystem(ZipNIO.java:19)
at com.jerney.ziptest.utils.ZipNIO.getFileSystem(ZipNIO.java:23)
at com.jerney.ziptest.App.main(App.java:15)

我尝试使用"jar:file:“、"file:/”和"file://“for URI.create()方法”,并且尝试在“URI.create”结尾添加"/“,但每次都得到相同的结果。我已经看到了另一种建议使用不同FileSystems工厂方法的解决方案,但我特别想使用这个构造函数,并且知道为什么这个构造函数不适合我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-21 07:02:29

每个FileSystemProvider都有自己的URI前缀。如果您使用file:前缀,您实际上是在请求默认的FileSystemProvider (取决于您的计算机是sun.nio.fs.UnixFileSystemProvider的实例还是sun.nio.fs.WindowsFileSystemProvider的实例)。

如果要使用ZipFileSystemProvider,则需要一个jar:前缀:

代码语言:javascript
复制
private static FileSystem createZipFileSystem(String zipFileName, boolean create) throws IOException {
    final Path path = Paths.get(zipFileName);
    final URI uri = URI.create("jar:" + path.toUri());
    final Map<String,String> env = new HashMap<String,String>();

    if(create) {
        env.put("create", "true");
    }

    return FileSystems.newFileSystem(uri, env);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43530119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档