Java FileSystemUtils-class And Method Code Example


Here's an example of how to use the FileSystemUtils class from the org.apache.commons.io package in Java to get information about the file system:

import org.apache.commons.io.FileSystemUtils;

public class Main {
    public static void main(String[] args) {
        long freeSpace = FileSystemUtils.freeSpaceKb("C:");
        long totalSpace = FileSystemUtils.sizeOfDirectory(new File("C:"));
        long usableSpace = FileSystemUtils.freeSpaceKb("C:") / FileSystemUtils.freeSpaceKb("C:")*totalSpace;
        
        System.out.println("Free space on C drive: " + freeSpace + " KB");
        System.out.println("Total space on C drive: " + totalSpace + " KB");
        System.out.println("Usable space on C drive: " + usableSpace + " KB");
    }
}

This example shows how to use the FileSystemUtils class to get information about the file system such as free space, total space, and usable space on the C drive. The freeSpaceKb() method is used to get the amount of free space in kilobytes, the sizeOfDirectory() method is used to get the total size of the directory in bytes and the percentage of free space is calculated by dividing free space by total space.

It's important to note that FileSystemUtils is part of the Apache Commons IO library and not part of the JDK.