Calculate Free Disk Space using Apache Commons IO jar
Jar required: Apache Commons IO
Download the jar: https://commons.apache.org/proper/commons-io/download_io.cgi
Class used: org.apache.commons.io.FileSystemUtils.
Sometimes we need to check the free disk size before copying some data from one location to another.
package com.java.tricks;
import java.io.IOException;
importorg.apache.commons.io.FileSystemUtils;
public class CalDiskSpace {
public static void main(String[] args) {
try {
// calculate free disk space in kilo-bytes - windows
double freeDskSpace = FileSystemUtils.freeSpaceKb("C:");
// FileSystemUtils.freeSpaceKb("/volume"); // unix
double freeDskInGb = freeDskSpace / (1024 * 1024);
System.out.println("Free Disk Space (Gb):" + freeDskInGb);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Free Disk Space (Gb):359.8280334472656
No comments:
Post a Comment