Java FileSystem-class And Method Code Example


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

import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileSystem;

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");
        
        //example of use of FileSystem
        FileSystem fileSystem = FileSystems.getDefault();
        String separator = fileSystem.getSeparator();
        System.out.println("File separator: " + separator);
    }
}

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, and how to use the FileSystem class to access information about the file system such as the file separator, which is the string that separates the names of files and directories in the file system.

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