0

我可以使用不受支持的 sun.awt.shell.ShellFolder 类来获取已安装的卷信息,但我们更愿意使用受支持的类。似乎 nio 类应该支持这一点,但我不知道如何。

我在 Win 7 上使用 Java 8 运行。使用下面的代码,ShellFolder 类提供有关已安装卷的以下信息:

  Z:\
  Title[0] = Name, val = MetrixAutomatedTestDataFiles (\\fcna02) (Z:)
  Title[1] = Size, val = Network Drive
  Title[2] = Item type, val = 1.46 TB
  Title[3] = Date modified, val = 645 GB
  Title[4] = Date created, val = NTFS
  Title[5] = Date accessed, val = 
  Title[6] = Attributes, val = \\fcna02\MetrixAutomatedTestDataFiles
  Title[7] = Offline status, val = ‎57%
  Title[8] = Offline availability, val = ‎645 GB free of 1.46 TB

第 6 项是我需要的信息。

当我尝试使用 nio FileStore 类时,除了最基本的信息外,它无法提供任何其他信息。

   (Z:)                   1572025140    895400568    676624572
    (Z:)
        attr=owner, Supported
        attr=owner, NOT VALID
        attr=dos, Supported
        attr=dos, NOT VALID
        attr=acl, Supported
        attr=acl, NOT VALID
        attr=basic, Supported
        attr=basic, NOT VALID
        attr=user, Supported
        attr=user, NOT VALID

获得 supportsFileAttributeView 为 true 的属性无效。

package com.metrixsoftware.build;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.util.Set;
import sun.awt.shell.ShellFolder;
import sun.awt.shell.ShellFolderColumnInfo;

public class Junk1 {

    public static void main(final String[] args) {
        System.out.println("--- Using ShellFolder ---");
        File[] roots = File.listRoots();

        for (File file : roots) {
            System.out.println(file.getAbsolutePath());
            try {
                ShellFolder sf = ShellFolder.getShellFolder(file);
                ShellFolderColumnInfo[] cols = sf.getFolderColumns();

                for (int idx = 0; (idx < cols.length); idx++) {
                    ShellFolderColumnInfo col = cols[idx];
                    Object val = sf.getFolderColumnValue(idx);
                    if (val != null) {
                        System.out.println("Title[" + idx + "] = " + col.getTitle() + ", val = " + val);
                    }
                }
            } catch (FileNotFoundException e) {
                System.out.println(" ERROR: " + e.getMessage());

            } finally {

            }
        }

        System.out.println("\n--- Using FileStore from Default FileSystems ---");
        Set<String> attributes = FileSystems.getDefault().supportedFileAttributeViews();

        for (FileStore fstore : FileSystems.getDefault().getFileStores()) {
            try {
                long total = fstore.getTotalSpace() / 1024;
                long used = (fstore.getTotalSpace() - fstore.getUnallocatedSpace()) / 1024;
                long avail = fstore.getUsableSpace() / 1024;
                System.out.format("%-20s %12d %12d %12d%n", fstore, total, used, avail);

                System.out.println("\t" + fstore);
                for (String attr : attributes) {
                    try {
                        if (fstore.supportsFileAttributeView(attr)) {
                            System.out.println("\t\tattr=" + attr + ", Supported");
                            try {
                                System.out.println("\t\tattr=" + attr + ", value=" + fstore.getAttribute(attr));
                            } catch (UnsupportedOperationException ex) {
                                System.out.println("\t\tattr=" + attr + ", NOT VALID");
                            }
                        } else {
                            System.out.println("\t\tattr=" + attr + ", NOT SUPPORTED");
                        }
                    } catch (IOException e) {
                        System.out.println("\t\tattr=" + attr + ", INVALID");
                    }
                }

            } catch (IOException e) {
                System.out.println(" ERROR: " + e.getMessage());

            } finally {

            }
        }
    }
}

有没有办法使用 nio 来获取卷挂载信息?

4

1 回答 1

0

Java 不提供访问挂载信息的内置方式。您将不得不使用Runtime.exec()来运行系统命令,例如Windows Diskpart

于 2019-06-14T19:27:05.680 回答