【问题标题】:Get FileStore object for any given path获取任何给定路径的 FileStore 对象
【发布时间】:2014-02-20 12:07:14
【问题描述】:

编辑:我认识到 FileSystem.getDefault() 会给我我在原始问题陈述中寻找的内容。我正在尝试使用 FileSystem.getFileSystem(URI) 来获取 any 给定路径的 FileSystem。

我正在尝试开发一些代码,为给定路径提供 java.nio.file.FileSystem 对象。

这里有一些非常简化的示例代码,可以更好地了解正在尝试的内容:

public FileSystem getCwdFilesystem()
{
    URI cwdUri = null;
    String delimiter = "";
    try
    {
        _cwd = System.getProperty("user.dir");
        cwdUri = new URI("file", delimiter + _cwd, null);
    }
    catch (URISyntaxException ue)
    {
        System.out.println("URI Creation failure on URI: " + _cwd);
        ue.printStackTrace();
        System.exit(1);
    }

    System.out.println("Filestore data for CWD: " + cwdUri.toString());

    return (FileSystems.getFileSystem(cwdUri));
}

执行时,最后一行代码抛出异常:

Filestore data for CWD: file:/Users/redacted/Documents/Java%20Projects/ExampleCode
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.getFileSystem(UnixFileSystemProvider.java:92)
    at java.nio.file.FileSystems.getFileSystem(FileSystems.java:217)
    at examplecode.FilesystemCapacity.getCwdFilesystem(FilesystemCapacity.java:54)
    at examplecode.FilesystemCapacity.main(FilesystemCapacity.java:33)
Java Result: 1

当我对分隔符变量进行小更新时:

String delimiter = "/";

我从同一个地方收到不同的错误消息:

Filestore data for CWD: file://Users/redacted/Documents/Java%20Projects/ExampleCode
Exception in thread "main" java.lang.IllegalArgumentException: Authority component present
    at sun.nio.fs.UnixFileSystemProvider.checkUri(UnixFileSystemProvider.java:73)
    at sun.nio.fs.UnixFileSystemProvider.getFileSystem(UnixFileSystemProvider.java:92)
    at java.nio.file.FileSystems.getFileSystem(FileSystems.java:217)
    at examplecode.FilesystemCapacity.getCwdFilesystem(FilesystemCapacity.java:54)
    at examplecode.FilesystemCapacity.main(FilesystemCapacity.java:33)
Java Result: 1

在分隔符中添加额外的“/”字符只会让我再次收到第一条错误消息。

我做错了什么?

【问题讨论】:

  • 如果添加“./”作为分隔符会怎样?
  • 不高兴。错误消息更改为“路径组件未定义”。
  • 你应该使用 File.separator 而不是 delimiter

标签: java linux macos os-agnostic


【解决方案1】:

我找到了一个我之前错过的 NIO.2 文档跟踪 on the last page 的参考。

我编写了一些测试代码,这些代码正是我所需要的:

    public void getPathFilesystem(String path)
    {
        try
        {
            URI rootURI = new URI("file:///");
            Path rootPath = Paths.get(rootURI);
            Path dirPath = rootPath.resolve(path);
            FileStore dirFileStore = Files.getFileStore(dirPath);

            printFileStore(dirFileStore, path);
        }
        catch (IOException | URISyntaxException e)
        {
            e.printStackTrace();
        }
    }

    public void printFileStore(FileStore filestore, String path)
    {
        try
        {
            System.out.println("Name: " + filestore.name());
            System.out.println("\tPath: " + path);
            System.out.println("\tSize: " + filestore.getTotalSpace());
            System.out.println("\tUnallocated: " + filestore.getUnallocatedSpace());
            System.out.println("\tUsable: " + filestore.getUsableSpace());
            System.out.println("\tType: " + filestore.type());
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

【讨论】:

  • 感谢resolve;文档确实说这样做很重要,但有点晦涩。
猜你喜欢
  • 1970-01-01
  • 2017-08-08
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2013-12-03
  • 2019-04-02
  • 1970-01-01
相关资源
最近更新 更多