如何将http .txt文件加载为Java FileSystem

时间:2017-07-02 17:59:36

标签: java file nio nio2

我正在攻读Java 1.8 OCP考试,我在Oracle学习指南第459页中讨论了一些内容,它说您可以将远程URL加载为FileSystem对象。我尝试过这个并且无法让它发挥作用。让这个有效的最简单的hello-world风格答案是什么?

import java.net.*;
import java.nio.file.*;
public class FileTest {
    public static void main(String[] args) throws URISyntaxException, MalformedURLException {
        FileSystem remoteFS = FileSystems.getFileSystem(
            new URI("http://www.gutenberg.org/files/55007")
        );
        Path remotePath = remoteFS.getPath("55007-0.txt");
        System.out.println(remotePath.isAbsolute());
    }
}

它抛出错误:

Exception in thread "main" java.nio.file.ProviderNotFoundException: 
  Provider "http" not found
    at java.nio.file.FileSystems.getFileSystem(FileSystems.java:224)
    at qa.test.FileTest.main(FileTest.java:7)

附录: FileSystemProvider.installedProviders()返回

[sun.nio.fs.MacOSXFileSystemProvider@4554617c,com.sun.nio.zipfs.ZipFileSystemProvider@74a14482]

0 个答案:

没有答案