Java : FileStore (Storage) 示例
Java 中的 FileStore (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 FileStore 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
The code examples on this page are run on Windows.
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
// --- PowerShell ---
//PS D:\> Get-PSDrive D
//
//Name Used (GB) Free (GB) Provider Root ...
//---- --------- --------- -------- ---- ...
//D 1597.26 265.63 FileSystem D:\ ...
final var total = store.getTotalSpace();
System.out.printf("%d GB%n", total / (1024 * 1024 * 1024)); // 1862 GB
final var free = store.getUnallocatedSpace();
System.out.printf("%d GB%n", free / (1024 * 1024 * 1024)); // 265 GB
final var used = total - free;
System.out.printf("%d GB%n", used / (1024 * 1024 * 1024)); // 1597 GB
Constructors
FileStore ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
abstract Object getAttribute (String attribute)
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
System.out.println(store.getAttribute("volume:isCdrom")); // false
final var path = Path.of("F:");
System.out.println(path); // F:
//PS D:\> Get-Volume F
//
//DriveLetter FriendlyName FileSystemType DriveType ...
//----------- ------------ -------------- --------- ...
//F DVD_ROM Unknown CD-ROM ...
final var store = Files.getFileStore(path);
System.out.println(store.type()); // UDF
System.out.println(store.getAttribute("volume:isCdrom")); // true
long getBlockSize ()
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.getBlockSize()); // 512
abstract <V extends FileStoreAttributeView> V getFileStoreAttributeView (Class<V> type)
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
final var view = store.getFileStoreAttributeView(FileStoreAttributeView.class);
System.out.println(view); // null
abstract long getTotalSpace ()
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
// --- PowerShell ---
//PS D:\> Get-PSDrive D
//
//Name Used (GB) Free (GB) Provider Root ...
//---- --------- --------- -------- ---- ...
//D 1597.26 265.63 FileSystem D:\ ...
System.out.println(store.getTotalSpace()); // 2000263573504
System.out.println(store.getUnallocatedSpace()); // 285218787328
System.out.println(store.getUsableSpace()); // 285218787328
final var path = Path.of("F:");
System.out.println(path); // F:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // UDF
// --- PowerShell ---
//PS D:\> Get-PSDrive F
//
//Name Used (GB) Free (GB) Provider Root ...
//---- --------- --------- -------- ---- ...
//F 0.05 0.00 FileSystem F:\ ...
System.out.println(store.getTotalSpace()); // 51329024
System.out.println(store.getUnallocatedSpace()); // 0
System.out.println(store.getUsableSpace()); // 0
abstract long getUnallocatedSpace ()
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
// --- PowerShell ---
//PS D:\> Get-PSDrive D
//
//Name Used (GB) Free (GB) Provider Root ...
//---- --------- --------- -------- ---- ...
//D 1597.26 265.63 FileSystem D:\ ...
System.out.println(store.getTotalSpace()); // 2000263573504
System.out.println(store.getUnallocatedSpace()); // 285218787328
System.out.println(store.getUsableSpace()); // 285218787328
final var path = Path.of("F:");
System.out.println(path); // F:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // UDF
// --- PowerShell ---
//PS D:\> Get-PSDrive F
//
//Name Used (GB) Free (GB) Provider Root ...
//---- --------- --------- -------- ---- ...
//F 0.05 0.00 FileSystem F:\ ...
System.out.println(store.getTotalSpace()); // 51329024
System.out.println(store.getUnallocatedSpace()); // 0
System.out.println(store.getUsableSpace()); // 0
abstract long getUsableSpace ()
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
// --- PowerShell ---
//PS D:\> Get-PSDrive D
//
//Name Used (GB) Free (GB) Provider Root ...
//---- --------- --------- -------- ---- ...
//D 1597.26 265.63 FileSystem D:\ ...
System.out.println(store.getTotalSpace()); // 2000263573504
System.out.println(store.getUnallocatedSpace()); // 285218787328
System.out.println(store.getUsableSpace()); // 285218787328
final var path = Path.of("F:");
System.out.println(path); // F:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // UDF
// --- PowerShell ---
//PS D:\> Get-PSDrive F
//
//Name Used (GB) Free (GB) Provider Root ...
//---- --------- --------- -------- ---- ...
//F 0.05 0.00 FileSystem F:\ ...
System.out.println(store.getTotalSpace()); // 51329024
System.out.println(store.getUnallocatedSpace()); // 0
System.out.println(store.getUsableSpace()); // 0
abstract boolean isReadOnly ()
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
System.out.println(store.isReadOnly()); // false
final var path = Path.of("F:");
System.out.println(path); // F:
//PS D:\> Get-Volume F
//
//DriveLetter FriendlyName FileSystemType DriveType ...
//----------- ------------ -------------- --------- ...
//F DVD_ROM Unknown CD-ROM ...
final var store = Files.getFileStore(path);
System.out.println(store.type()); // UDF
System.out.println(store.isReadOnly()); // true
abstract String name ()
// --- PowerShell ---
//PS D:\> Get-Volume W
//
//DriveLetter FriendlyName FileSystemType ...
//----------- ------------ -------------- ...
//W Work NTFS ...
final var path = Path.of("W:");
System.out.println(path); // W:
final var store = Files.getFileStore(path);
System.out.println(store.name()); // Work
System.out.println(store.type()); // NTFS
abstract boolean supportsFileAttributeView (Class<? extends FileAttributeView> type)
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
final var dos = store.supportsFileAttributeView(DosFileAttributeView.class);
System.out.println(dos); // true
final var posix = store.supportsFileAttributeView(PosixFileAttributeView.class);
System.out.println(posix); // false
abstract boolean supportsFileAttributeView (String name)
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
final var dos = store.supportsFileAttributeView("dos");
System.out.println(dos); // true
final var posix = store.supportsFileAttributeView("posix");
System.out.println(posix); // false
abstract String type ()
final var path = Path.of("D:");
System.out.println(path); // D:
final var store = Files.getFileStore(path);
System.out.println(store.type()); // NTFS
final var path = Path.of("F:");
System.out.println(path); // F:
//PS D:\> Get-Volume F
//
//DriveLetter FriendlyName FileSystemType DriveType ...
//----------- ------------ -------------- --------- ...
//F DVD_ROM Unknown CD-ROM ...
final var store = Files.getFileStore(path);
System.out.println(store.type()); // UDF