How to Retrieve Free Disk Space in Java


In this example we will show how to get free disk space in Java.

Source Code

package com.beginner.examples;

import java.io.*;

public class FileOperator24Example { 
	public static void main(String[] args) throws IOException{

		File file = new File("c:");
    	long totalSpace = file.getTotalSpace();
    	long usableSpace = file.getUsableSpace();
    	System.out.println("Total size : " + totalSpace + " B");
    	System.out.println("Usable size : " + usableSpace + " B");

    	System.out.println("Total size : " + totalSpace/1024/1024/1024 + " GB");
    	System.out.println("Usable size : " + usableSpace/1024/1024/1024 + " GB");
    }
}

Output:

Total size : 185107214336 B
Usable size : 97401974784 B
Total size : 172 GB
Usable size : 90 GB
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments