Apache Commons IO - FileSystemUtils Class



Overview

FileSystemUtils class provides method to get the free space on a disk drive.

Class Declaration

Following is the declaration for org.apache.commons.io.FileSystemUtils Class −

public class FileSystemUtils
   extends Object

Example - Getting Free Space of a drive in Bytes

CommonsIoTester.java

package com.tutorialspoint;

import java.io.IOException;

import org.apache.commons.io.FileSystemUtils;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      System.out.println("Free Space " + FileSystemUtils.freeSpace("C:/") + " Bytes");
   }
}

Output

It will print the following result −

Free Space 186157182976 Bytes

Example - Getting Free Space of a drive in KiloBytes

CommonsIoTester.java

package com.tutorialspoint;

import java.io.IOException;

import org.apache.commons.io.FileSystemUtils;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      System.out.println("Free Space " + FileSystemUtils.freeSpaceKb("C:/") + " KB");
   }
}

Output

It will print the following result −

Free Space 181794512 KB

Example - Getting Free Space of a drive in KiloBytes with timeout

CommonsIoTester.java

package com.tutorialspoint;

import java.io.IOException;

import org.apache.commons.io.FileSystemUtils;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      System.out.println("Free Space " + FileSystemUtils.freeSpaceKb("C:/",1000) + " KB");
   }
}

Output

It will print the following result −

Free Space 181794512 KB
Advertisements