Java : FileStore (Storage) with Examples

FileStore (Java SE 22 & JDK 22) in Java with Examples.
You will find code samples for most of the FileStore methods.


Summary

Storage for files. A FileStore represents a storage pool, device, partition, volume, concrete file system or other implementation specific means of file storage.

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

Initializes a new instance of this class.

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)

Reads the value of a file store 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 ()

Returns the number of bytes per block in this file store.

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)

Returns a FileStoreAttributeView of the given 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 ()

Returns the size, in bytes, of the file store.

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

Returns the number of unallocated bytes in the file store.

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

Returns the number of bytes available to this Java virtual machine on the file store.

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

Tells whether this file store is read-only.

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

Returns the name of this file store.

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

Tells whether or not this file store supports the file attributes identified by the given file attribute view.

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)

Tells whether or not this file store supports the file attributes identified by the given file attribute view.

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

Returns the type of this file store.

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

Related posts

To top of page