Java : FileStore (Storage) 示例

Java 中的 FileStore (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 FileStore 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

文件存储。FileStore 表示存储池、设备、分区、卷、具体文件系统或其他特定于文件存储实现的方式。 (机器翻译)

Class diagram

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)

返回给定类型的 FileStoreAttributeView。 (机器翻译)

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

返回此 Java 虚拟机在文件存储上可用的字节数。 (机器翻译)

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

相关文章

To top of page