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究竟是啥?


POPMUISE
浏览 718回答 1
1回答

万千封印

找到一篇文章可以参考一下:NIO.2 Filesystem APIAbstract FileSystemProvider class stores immutable list of allinstalled file system providers starting with default provider locatedat first index. This list is also publicly available by callinginstalledProviders method. Based on this description one can see, thatFileSystemProvider is typical implementation of factory designpattern.按这个描述来看FileSystemProvider实现的是一个工厂模式。你这应该是没有找到对应的处理rar格式文件的FileSystemProvider。google的一下说java7有实现zip格式的,不过没找到rar的,估计得自己写一个。zip的参考这里。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java