一般来讲,要用java得到硬盘空间,有3种方法:
  1. 调用system的command,然后分析得到的结果,这种方法有很强的系统依赖性,linux下和win下要分别写程序
  下面是一个win下的例子,编译成功之后,运行java Diskspace yourdir(比如c:\)

 
  
  1.   import java.io.BufferedReader;  
  2.   import java.io.InputStreamReader;  
  3.     
  4.   /**  
  5.   * Determine free disk space for a given directory by 
  6.   * parsing the output of the dir command.  
  7.   * This class is inspired by the code at 
  8.   * Works only under Windows under certain circumstances.  
  9.   * Yes, it's that shaky.  
  10.   * Requires Java 1.4 or higher.  
  11.   * @[EMAIL PROTECTED]  
  12.   *Marco Schmidt  
  13.   */  
  14.   public class Diskspace  
  15.   {  
  16.   private Diskspace()  
  17.   {  
  18.   // prevent instantiation of this class  
  19.   }  
  20.     
  21.   /**  
  22.   * Return available free disk space for a directory.  
  23.   * @[EMAIL PROTECTED]  
  24.   dirName name of the directory  
  25.   * @[EMAIL PROTECTED]  
  26.   free disk space in bytes or -1 if unknown  
  27.   */  
  28.   public static long getFreeDiskSpace(String dirName)  
  29.   {  
  30.   try  
  31.   {  
  32.   // guess correct 'dir' command by looking at the  
  33.   // operating system name 
  34.   String os = System.getProperty("os.name");  
  35.   String command;  
  36.   if (os.equals("Windows NT") ||  
  37.   os.equals("Windows 2000"))  
  38.   {  
  39.   command = "cmd.exe /c dir " + dirName;  
  40.   }  
  41.   else 
  42.   {  
  43.   command = "command.com /c dir " + dirName;  
  44.   }  
  45.   // run the dir command on the argument directory name 
  46.   Runtime runtime = Runtime.getRuntime();  
  47.   Process process = ;  
  48.   process = runtime.exec(command);  
  49.   if (process == )  
  50.   {  
  51.   return -1;  
  52.   }  
  53.   // read the output of the dir command  
  54.   // only the last line is of interest  
  55.   BufferedReader in = new BufferedReader(  
  56.   new InputStreamReader(process.getInputStream()));  
  57.   String line;  
  58.   String freeSpace = ;  
  59.   while ((line = in.readLine()) != )  
  60.   {  
  61.   freeSpace = line;  
  62.   }  
  63.   if (freeSpace == )  
  64.   {  
  65.   return -1;  
  66.   }  
  67.   process.destroy();  
  68.   // remove dots & commas & leading and trailing whitespace  
  69.   freeSpace = freeSpace.trim();  
  70.   freeSpace = freeSpace.replaceAll("\\.""");  
  71.   freeSpace = freeSpace.replaceAll(",""");  
  72.   String[] items = freeSpace.split(" ");  
  73.   // the first valid numeric value in items after(!) index 0  
  74.   // is probably the free disk space 
  75.   int index = 1;  
  76.   while (index < items.length)  
  77.   {  
  78.   try  
  79.   {  
  80.   long bytes = Long.parseLong(items[index++]);  
  81.   return bytes;  
  82.   }  
  83.   catch (NumberFormatException nfe)  
  84.   {  
  85.   }  
  86.   }  
  87.   return -1;  
  88.   }  
  89.   catch (Exception exception)  
  90.   {  
  91.   return -1;  
  92.   }  
  93.   }  
  94.     
  95.   /**  
  96.   * Command line program to print the free diskspace to stdout  
  97.   * for all 26 potential root directories A:\ to Z:* (when no parameters are given to this program)  
  98.   * or for those directories (drives) specified as parameters.  
  99.   * @[EMAIL PROTECTED]  
  100.   args program parameters  
  101.   */  
  102.   public static void main(String[] args)  
  103.   {  
  104.   if (args.length == 0)  
  105.   {  
  106.   for (char c = 'A'; c <= 'Z'; c++)  
  107.   {  
  108.   String dirName = c + ":\\";  
  109.   System.out.println(dirName + " " +  
  110.   getFreeDiskSpace(dirName));  
  111.   }  
  112.   }  
  113.   else 
  114.   {  
  115.   for (int i = 0; i < args.length; i++)  
  116.   {  
  117.   System.out.println(args[i] + " " +  
  118.   getFreeDiskSpace(args[i]));  
  119.   }  
  120.   }  
  121.   }  
  122.   }  

  
  方法二:使用Jconfig,可以跨平台
  从http://www.tolstoy.com/samizdat/jconfig.html上下载jconfig.
  下载的包的sample里有很简单的例子,如果是要得到磁盘空间的话:
  用FileRegistry.getVolumes()得到DiskVolume
  然后call getFreeSpace()和getMaxCapacity()
  就是这么简单..:)
  
  方法三:jni
  这个是解决所有和os相关的操作的万能利器了.
  例子我也懒得写了.
  写一个dll然后call之即可.


转自:http://www.oracle-bbs.com/oracle_forum_RuHeYongJavaDeiDaoYingPanKongJian-sql-database-0-288535.html