我可以使用不受支持的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获取卷挂载信息?