Tell how much free space is on disk in Java

Unfortunately, there is no API or method to find out, not even in JDK 1.3. Check out BUG 4057701 in Sun’s Bug Parade. Until this bug is fixed, you can try out to execute an external command like dir (Windows) or du (unix) and parse the output:

import java.util.*;
import java.text.*;
import java.io.*;
  
public class Main
{
   public static void main(String [] args) throws Exception {
      long fs = getFreeSpace("c:");
 
      System.out.println("Free space: " + fs);
   }
 
   public static long getFreeSpace(String dir) throws Exception {
      String osname = System.getProperty("os.name");
      String command = "";
      if (osname.indexOf("NT") > -1)
         command = "c:\winnt\cmd.com";
      else if (osname.indexOf("Windows") > -1)
         command = "c:\windows\command.com";
 
      Process p = Runtime.getRuntime().exec(command + " /c dir " + dir + " > c:\dir.txt");
      p.waitFor(); 
 
      File f = new File("c:\dir.txt");
      BufferedReader br = new BufferedReader(new FileReader(f));
      String line;
      String totalBytes = "";
      while ((line = br.readLine()) != null) {
         if (line.indexOf("bytes free") > -1) {
            int start = line.indexOf("dir(s)") + 7;
            int end = line.indexOf("bytes free", start);
            totalBytes = line.substring(start, end).trim();
            break;
         }
      }
 
      br.close();
      f.delete();
 
      return NumberFormat.getInstance().parse(totalBytes).longValue(); 
   }
}

This code needs to be adapted to be language and platform independent!