Get File System Information in Java using OSHI library
Tags: OSHI File System
Introduction
In this tutorial, we learn how to access file system information in Java applications with OSHI library. By different Java examples we show how to get information on file storage of a computer system.
Add OSHI library to the Java project
To use OSHI Java library in the Gradle build project, add the following dependency into the build.gradle file.
compile group: 'com.github.oshi', name: 'oshi-core', version: '5.3.4'
To use OSHI Java library in the Maven build project, add the following dependency into the pom.xml file.
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>5.3.4</version>
</dependency>
For more information about the OSHI library you can visit the library repository at github.com/oshi/oshi
How to use OSHI library to get File System information
To use the library we need to instantiate an object of SystemInfo class as this is the main entry point of OSHI library.
SystemInfo systemInfo = new SystemInfo();
Then get the object of OperatingSystem in order to access file system
OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
Get the FileSystem and OSFileStore objects
FileSystem fileSystem = operatingSystem.getFileSystem();
List<OSFileStore> osFileStores = fileSystem.getFileStores();
Example Java program to show operating system file storage
In the following Java example we show how to get information about the storage pool, device or partition of the file system on the computer that the Java program is running on.
import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import java.util.List;
public class FileSystemExample1 {
public static void main(String... args) {
SystemInfo systemInfo = new SystemInfo();
OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
FileSystem fileSystem = operatingSystem.getFileSystem();
List<OSFileStore> osFileStores = fileSystem.getFileStores();
for(OSFileStore fileStore : osFileStores) {
System.out.println(fileStore.toString());
}
}
}
OSFileStore [name=Local Fixed Disk (C:), volume=\\?\Volume{f0b491ae-e7b3-444d-aeb4-665544332211}\, label=Windows, logicalVolume=, mount=C:\, description=Fixed drive, fsType=NTFS, options="rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp", uuid=f0b491ae-e7b3-444d-aeb4-665544332211, freeSpace=124230467584, usableSpace=124230467584, totalSpace=511232061440, freeInodes=0, totalInodes=0]
OSFileStore [name=Local Fixed Disk (D:), volume=\\?\Volume{85a6e7b3-ef7e-4f10-b4b9-665544332211}\, label=DATA, logicalVolume=, mount=D:\, description=Fixed drive, fsType=NTFS, options="rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp", uuid=85a6e7b3-ef7e-4f10-b4b9-665544332211, freeSpace=4638666752, usableSpace=4638666752, totalSpace=1000203087872, freeInodes=0, totalInodes=0]
Example Java program show detail of operating system file storage
The below Java example to access specific details for the operating system file storage.
import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.util.FormatUtil;
import java.util.List;
public class FileSystemExample2 {
public static void main(String... args) {
SystemInfo systemInfo = new SystemInfo();
OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
FileSystem fileSystem = operatingSystem.getFileSystem();
List<OSFileStore> osFileStores = fileSystem.getFileStores();
for(OSFileStore fileStore : osFileStores) {
System.out.println("Description: " + fileStore.getDescription());
System.out.println("Label: " + fileStore.getLabel());
System.out.println("Logical Volume: " + fileStore.getLogicalVolume());
System.out.println("Mount: " + fileStore.getMount());
System.out.println("Name: " + fileStore.getName());
System.out.println("Options: " + fileStore.getOptions());
System.out.println("Type: " + fileStore.getType());
System.out.println("UUID: " + fileStore.getUUID());
System.out.println("Volume: " + fileStore.getVolume());
System.out.println("Free Space: " + FormatUtil.formatBytes(fileStore.getFreeSpace()));
System.out.println("Total Space: " + FormatUtil.formatBytes(fileStore.getTotalSpace()));
System.out.println("Usable Space: " + FormatUtil.formatBytes(fileStore.getUsableSpace()));
System.out.println();
}
}
}
Description: Fixed drive
Label: Windows
Logical Volume:
Mount: C:\
Name: Local Fixed Disk (C:)
Options: rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp
Type: NTFS
UUID: f0b491ae-e7b3-444d-aeb4-665544332211
Volume: \\?\Volume{f0b491ae-e7b3-444d-aeb4-665544332211}\
Free Space: 115.7 GiB
Total Space: 476.1 GiB
Usable Space: 115.7 GiB
Description: Fixed drive
Label: DATA
Logical Volume:
Mount: D:\
Name: Local Fixed Disk (D:)
Options: rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp
Type: NTFS
UUID: 85a6e7b3-ef7e-4f10-b4b9-665544332211
Volume: \\?\Volume{85a6e7b3-ef7e-4f10-b4b9-665544332211}\
Free Space: 4.3 GiB
Total Space: 931.5 GiB
Usable Space: 4.3 GiB
Happy Coding 😊
Related Articles
Get Physical Memory or RAM Information in Java using OSHI library
Get Operating System Information in Java using OSHI library