JNA-based operating system and hardware information library for Java providing cross-platform access to system metrics
—
File system information, mount points, disk usage statistics, and file store details with comprehensive storage metrics.
Interface providing access to file system information and mounted file stores.
interface FileSystem {
List<OSFileStore> getFileStores();
List<OSFileStore> getFileStores(boolean localOnly);
long getOpenFileDescriptors();
long getMaxFileDescriptors();
}getFileStores() - Gets all mounted file storesgetFileStores(boolean localOnly) - Gets file stores, optionally filtering to local onlygetOpenFileDescriptors() - Gets the current number of open file descriptorsgetMaxFileDescriptors() - Gets the maximum number of file descriptors allowedRepresents a mounted file system or file store with detailed storage information.
interface OSFileStore {
String getName();
String getVolume();
String getLabel();
String getLogicalVolume();
String getMount();
String getDescription();
String getType();
String getOptions();
String getUUID();
long getFreeSpace();
long getUsableSpace();
long getTotalSpace();
long getFreeInodes();
long getTotalInodes();
boolean updateAttributes();
}getName() - Gets the file store namegetVolume() - Gets the volume namegetLabel() - Gets the volume labelgetLogicalVolume() - Gets the logical volume namegetMount() - Gets the mount point pathgetDescription() - Gets a description of the file storegetType() - Gets the file system type (e.g., "NTFS", "ext4", "HFS+")getOptions() - Gets mount options as a stringgetUUID() - Gets the unique identifier for the file storegetFreeSpace() - Gets free space in bytesgetUsableSpace() - Gets usable space in bytes (may be less than free space due to reserved space)getTotalSpace() - Gets total space in bytesgetFreeInodes() - Gets the number of free inodesgetTotalInodes() - Gets the total number of inodesupdateAttributes() - Updates the file store information and returns true if successfulimport oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
List<OSFileStore> fileStores = fileSystem.getFileStores();
System.out.println("=== File Systems ===");
for (OSFileStore store : fileStores) {
System.out.printf("Name: %s%n", store.getName());
System.out.printf(" Mount: %s%n", store.getMount());
System.out.printf(" Type: %s%n", store.getType());
System.out.printf(" Total: %s%n", formatBytes(store.getTotalSpace()));
System.out.printf(" Free: %s%n", formatBytes(store.getFreeSpace()));
System.out.printf(" Usable: %s%n", formatBytes(store.getUsableSpace()));
System.out.printf(" Usage: %.1f%%%n%n",
(store.getTotalSpace() - store.getFreeSpace()) * 100.0 / store.getTotalSpace());
}
private static String formatBytes(long bytes) {
if (bytes < 1024) return bytes + " B";
if (bytes < 1024 * 1024) return String.format("%.1f KB", bytes / 1024.0);
if (bytes < 1024 * 1024 * 1024) return String.format("%.1f MB", bytes / 1024.0 / 1024.0);
return String.format("%.1f GB", bytes / 1024.0 / 1024.0 / 1024.0);
}import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
// Get only local file systems (excludes network mounts)
List<OSFileStore> localStores = fileSystem.getFileStores(true);
System.out.println("=== Local File Systems ===");
for (OSFileStore store : localStores) {
System.out.printf("%-20s %-10s %s%n",
store.getName(),
store.getType(),
store.getMount()
);
}import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
List<OSFileStore> fileStores = fileSystem.getFileStores();
long totalSpace = 0;
long freeSpace = 0;
long usableSpace = 0;
System.out.println("=== Disk Usage Summary ===");
System.out.printf("%-30s %10s %10s %10s %8s%n",
"Mount Point", "Total", "Free", "Usable", "Used %");
System.out.println("-".repeat(70));
for (OSFileStore store : fileStores) {
totalSpace += store.getTotalSpace();
freeSpace += store.getFreeSpace();
usableSpace += store.getUsableSpace();
double usedPercent = (store.getTotalSpace() - store.getFreeSpace()) * 100.0 / store.getTotalSpace();
System.out.printf("%-30s %10s %10s %10s %7.1f%%%n",
store.getMount(),
formatSize(store.getTotalSpace()),
formatSize(store.getFreeSpace()),
formatSize(store.getUsableSpace()),
usedPercent
);
}
System.out.println("-".repeat(70));
System.out.printf("%-30s %10s %10s %10s %7.1f%%%n",
"TOTAL",
formatSize(totalSpace),
formatSize(freeSpace),
formatSize(usableSpace),
(totalSpace - freeSpace) * 100.0 / totalSpace
);
private static String formatSize(long bytes) {
if (bytes < 1024L * 1024L * 1024L) {
return String.format("%.1f MB", bytes / 1024.0 / 1024.0);
} else if (bytes < 1024L * 1024L * 1024L * 1024L) {
return String.format("%.1f GB", bytes / 1024.0 / 1024.0 / 1024.0);
} else {
return String.format("%.1f TB", bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0);
}
}import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
List<OSFileStore> fileStores = fileSystem.getFileStores();
for (OSFileStore store : fileStores) {
System.out.println("=== File Store Details ===");
System.out.println("Name: " + store.getName());
System.out.println("Volume: " + store.getVolume());
System.out.println("Label: " + store.getLabel());
System.out.println("Logical Volume: " + store.getLogicalVolume());
System.out.println("Mount Point: " + store.getMount());
System.out.println("Description: " + store.getDescription());
System.out.println("File System Type: " + store.getType());
System.out.println("Mount Options: " + store.getOptions());
System.out.println("UUID: " + store.getUUID());
System.out.println("\n--- Space Information ---");
System.out.println("Total Space: " + formatBytes(store.getTotalSpace()));
System.out.println("Free Space: " + formatBytes(store.getFreeSpace()));
System.out.println("Usable Space: " + formatBytes(store.getUsableSpace()));
System.out.println("Used Space: " + formatBytes(store.getTotalSpace() - store.getFreeSpace()));
if (store.getTotalInodes() > 0) {
System.out.println("\n--- Inode Information ---");
System.out.println("Total Inodes: " + store.getTotalInodes());
System.out.println("Free Inodes: " + store.getFreeInodes());
System.out.println("Used Inodes: " + (store.getTotalInodes() - store.getFreeInodes()));
System.out.printf("Inode Usage: %.1f%%%n",
(store.getTotalInodes() - store.getFreeInodes()) * 100.0 / store.getTotalInodes());
}
System.out.println();
}import oshi.SystemInfo;
import oshi.software.os.FileSystem;
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
long openFDs = fileSystem.getOpenFileDescriptors();
long maxFDs = fileSystem.getMaxFileDescriptors();
System.out.println("=== File Descriptor Usage ===");
System.out.println("Open File Descriptors: " + openFDs);
System.out.println("Maximum File Descriptors: " + maxFDs);
if (maxFDs > 0) {
double usage = openFDs * 100.0 / maxFDs;
System.out.printf("File Descriptor Usage: %.1f%%%n", usage);
if (usage > 80) {
System.out.println("WARNING: High file descriptor usage!");
}
}import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
// Monitor specific file system
String targetMount = "/"; // or "C:\\" on Windows
OSFileStore targetStore = null;
for (OSFileStore store : fileSystem.getFileStores()) {
if (store.getMount().equals(targetMount)) {
targetStore = store;
break;
}
}
if (targetStore != null) {
System.out.println("Monitoring file system: " + targetStore.getMount());
long initialFree = targetStore.getFreeSpace();
System.out.println("Initial free space: " + formatBytes(initialFree));
try {
Thread.sleep(5000); // Wait 5 seconds
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Update and check again
if (targetStore.updateAttributes()) {
long currentFree = targetStore.getFreeSpace();
long change = currentFree - initialFree;
System.out.println("Current free space: " + formatBytes(currentFree));
if (change != 0) {
System.out.printf("Change: %s (%+d bytes)%n",
formatBytes(Math.abs(change)), change);
} else {
System.out.println("No change detected");
}
} else {
System.out.println("Failed to update file store attributes");
}
}import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
List<OSFileStore> fileStores = fileSystem.getFileStores();
// Group by file system type
Map<String, List<OSFileStore>> storesByType = fileStores.stream()
.collect(Collectors.groupingBy(OSFileStore::getType));
System.out.println("=== File Systems by Type ===");
for (Map.Entry<String, List<OSFileStore>> entry : storesByType.entrySet()) {
String type = entry.getKey();
List<OSFileStore> stores = entry.getValue();
System.out.printf("%s (%d mount%s):%n",
type, stores.size(), stores.size() == 1 ? "" : "s");
for (OSFileStore store : stores) {
System.out.printf(" %-30s %s%n", store.getMount(), formatBytes(store.getTotalSpace()));
}
System.out.println();
}Install with Tessl CLI
npx tessl i tessl/maven-com-github-oshi--oshi-core