java - 获取任何给定路径的 FileStore 对象

标签 java linux macos os-agnostic

编辑:我知道 FileSystem.getDefault() 会给我在我原来的问题陈述中寻找的东西。我正在尝试使用 FileSystem.getFileSystem(URI) 获取任何给定路径的文件系统。

我正在尝试开发一些代码,为我提供给定路径的 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

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

我做错了什么?

最佳答案

我找到了一个我以前错过的引用 on the last page NIO.2 文档轨迹。

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

    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();
        }
    }

关于java - 获取任何给定路径的 FileStore 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21418358/

相关文章:

Linux 脚本循环

linux - Boost 找不到本地安装的 icu

perl - Mac 上的 GIT-SVN 问题

java - "package private"成员访问不是默认(无修饰符)访问的同义词吗?

java - java中通过数组分割字段

c - `(void) inotify_rm_watch(fd, wd)` 和 `inotify_rm_watch(fd, wd)` 之间的区别

c++ - Mac OSX 下 Eclipse CDT 与 Qt 交叉编译

swift - 使用 "Unknown Command Type <NSBatchDeleteRequest,..>"执行 NSBatchDeleteRequest 时应用程序崩溃

java - 用 Java 创建新字体

java - JasperReport 大小限制