首页 > Java里的Provider是什么?

Java里的Provider是什么?

敲了一段修改压缩文件内容的代码,如下:

public static void rarWriteTest() {
        Path tempRar = Paths.get("D:\\copy.rar");
        try (FileSystem workingFs =
            FileSystems.newFileSystem(tempRar, null)) {
            Path pathForFile = workingFs.getPath("/hello.txt");
            List<String> ls = new ArrayList<>();
            ls.add("Hello World!");

            Files.write(pathForFile, ls, Charset.defaultCharset(),
                        StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

运行报如下错:

Exception in thread "main" java.nio.file.ProviderNotFoundException: Provider not found
    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:407)
    at io.StaticDemos.rarWriteTest(StaticDemos.java:43)
    at io.StaticDemos.main(StaticDemos.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

查了一下api,api这样写:

ProviderNotFoundException - if a provider supporting this file type cannot be located

然而并不明白啊,provider究竟是啥?


找到一篇文章可以参考一下:
NIO.2 Filesystem API

Abstract FileSystemProvider class stores immutable list of all
installed file system providers starting with default provider located
at first index. This list is also publicly available by calling
installedProviders method. Based on this description one can see, that
FileSystemProvider is typical implementation of factory design
pattern.

按这个描述来看FileSystemProvider实现的是一个工厂模式。你这应该是没有找到对应的处理rar格式文件的FileSystemProvider。
google的一下说java7有实现zip格式的,不过没找到rar的,估计得自己写一个。
zip的参考这里。

【热门文章】
【热门文章】