Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total disk space in Linux with Java?

I am able to get free disk space. How would I get the total disk space?

My code is:

import java.io.IOException;

import org.apache.commons.io.FileSystemUtils;

public class DiskSpace {
    public static void main(String[] args) {
        try {

            //calculate free disk space
            double freeDiskSpace = FileSystemUtils.freeSpaceKb(args[0]);
            System.out.println(args[0]);

            //convert the number into gigabyte
            double freeDiskSpaceGB = freeDiskSpace / 1024 / 1024;

            System.out.println("Free Disk Space (GB):" + freeDiskSpaceGB);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

FileSystemUtils do not have method for total space of disk ??? hopes for your reply

Thanks in advance

like image 569
user1237231 Avatar asked Feb 28 '12 06:02

user1237231


2 Answers

If you're using Java6 the File class will do:

public long getTotalSpace()
public long getFreeSpace()
public long getUsableSpace()
like image 134
Amir Afghani Avatar answered Sep 22 '22 19:09

Amir Afghani


try

  System.out.println(new File("/").getTotalSpace()/1024/1024/1024); //in GB
like image 36
Nirmal- thInk beYond Avatar answered Sep 20 '22 19:09

Nirmal- thInk beYond