Java : FileStore (ストレージ) - API使用例
FileStore (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
FileStore クラスを使うと、ストレージの合計容量や空き容量を取得することができます。
ストレージとは、例えば Windows では C:ドライブ や D:ドライブ などのことをいいます。
※本記事のコード例は 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
コンストラクタ
FileStore ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
メソッド
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