Java Disk Free Space Get getUnixFreeSpace(String pathname)

Here you can find the source of getUnixFreeSpace(String pathname)

Description

get Unix Free Space

License

BSD License

Declaration

public static long getUnixFreeSpace(String pathname) throws IOException 

Method Source Code


//package com.java2s;
// BSD License (http://www.galagosearch.org/license)

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static long getUnixFreeSpace(String pathname) throws IOException {
        try {//from  w w w .  j  ava 2  s.  c  o  m
            // BUGBUG: will not work on windows
            String[] command = { "df", "-Pk", pathname };
            Process process = Runtime.getRuntime().exec(command);
            InputStream procOutput = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(procOutput));

            // skip the first line
            reader.readLine();
            String line = reader.readLine();
            String[] fields = line.split("\\s+");
            reader.close();

            process.getErrorStream().close();
            process.getInputStream().close();
            process.getOutputStream().close();
            process.waitFor();

            long freeSpace = Long.parseLong(fields[3]) * 1024;
            return freeSpace;
        } catch (InterruptedException ex) {
            return 0;
        }
    }
}

Related

  1. getFreeSpace(String dir)
  2. getFreeSpace(String pathname)
  3. getFreeSpaceOnPartition(File f)
  4. getTreeSize(File root)
  5. getTreeSize(File root)