站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题Java编程获取硬盘空间

Java编程获取硬盘空间

摘要: 一般来讲,要用java得到硬盘空间,有3种方法
<table cellSpacing=0 width="85%" align=center border=1> <tr> <td height=19>  Java关键字</td></tr> <tr> <td height=19>网络 J2ME 手机游戏 JavaCard Struts 游戏 分析器 JAAS EJB JavaMail 设计模式 J2EE </td></tr> <tr> <td height=15>XML EJB JavaBean Servlet JDBC </td></tr></table>

  因为论坛有人问到这个问题,所以就写了这篇文章。希望对大家有所帮助。

  一般来讲,要用java得到硬盘空间,有3种方法:

  1. 调用system的command,然后分析得到的结果,这种方法有很强的系统依赖性,linux下和win下要分别写程序。

<iframe width="360" height="300" align="center"scrolling="No" frameborder="0" marginheight="0" marginwidth="0" SRC="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html"></iframe>

  下面是一个win下的例子,编译成功之后,运行java Diskspace yourdir(比如c:\)

<table width="100%" bgColor=#ffffff> <tr> <td>import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* Determine free disk space for a given directory by
* parsing the output of the dir command.
* This class is inspired by the code at
* Works only under Windows under certain circumstances.
* Yes, it's that shaky.
* Requires Java 1.4 or higher.
* @[EMAIL PROTECTED]
*Marco Schmidt
*/
public class Diskspace
{
 private Diskspace()
 {
  // prevent instantiation of this class
 }

/**
* Return available free disk space for a directory.
* @[EMAIL PROTECTED]
dirName name of the directory
* @[EMAIL PROTECTED]
free disk space in bytes or -1 if unknown
*/

 public static long getFreeDiskSpace(String dirName)
 {
  try
  {
   // guess correct 'dir' command by looking at the
   // operating system name
   String os = System.getProperty("os.name");
   String command;
   if (os.equals("Windows NT") ||os.equals("Windows 2000"))
   {
    command = "cmd.exe /c dir " + dirName;
   }
   else
   {
    command = "command.com /c dir " + dirName;
   }
   // run the dir command on the argument directory name
   Runtime runtime = Runtime.getRuntime();
   Process process = null;
   process = runtime.exec(command);
   if (process == null)
   {
    return -1;
   }
   // read the output of the dir command
   // only the last line is of interest
   BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
   String line;
   String freeSpace = null;
   while ((line = in.readLine()) != null)
   {
    freeSpace = line;
   }
   if (freeSpace == null)
   {
    return -1;
   }
   process.destroy();

   // remove dots & commas & leading and trailing whitespace
   freeSpace = freeSpace.trim();
   freeSpace = freeSpace.replaceAll("\\.", "");
   freeSpace = freeSpace.replaceAll(",", "");
   String[] items = freeSpace.split(" ");
   // the first valid numeric value in items after(!) index 0
   // is probably the free disk space
   int index = 1;
   while (index < items.length)
   {
    try
    {
     long bytes = Long.parseLong(items[index++]);
     return bytes;
    }
    catch (NumberFormatException nfe)
    {
     }
   }
   return -1;
  }
  catch (Exception exception)
  {
    return -1;
  }
 }

/**
* Command line program to print the free diskspace to stdout
* for all 26 potential root directories A:\ to Z:\
* (when no parameters are given to this program)
* or for those directories (drives) specified as parameters.
* @[EMAIL PROTECTED]
args program parameters
*/
 
 public static void main(String[] args)
 {
  if (args.length == 0)
  {
   for (char c = 'A'; c <= 'Z'; c++)
   {
    String dirName = c + ":\\";
    System.out.println(dirName + " " +
    getFreeDiskSpace(dirName));
   }
  }
  else
  {
   for (int i = 0; i < args.length; i++)
   {
    System.out.println(args[i] + " " + getFreeDiskSpace(args[i]));
   }
  }
 }
}

</td></tr></table>

  方法二:使用Jconfig,可以跨平台

  从http://www.tolstoy.com/samizdat/jconfig.html上下载jconfig。

  下载的包的sample里有很简单的例子,如果是要得到磁盘空间的话:

  用FileRegistry.getVolumes()得到DiskVolume。

  然后call getFreeSpace()和getMaxCapacity()。

  就是这么简单。

  方法三:jni

  这个是解决所有和os相关的操作的万能利器了。

  例子我也懒得写了。

  写一个dll然后call之即可。

<table width="570" border="0" cellspacing="0" cellpadding="5" bgcolor="FBE392"> <tr> <td> <table width="570" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="E1B004" width="200" align="center">天极IT资讯短信服务 电脑小技巧
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="fffcc0"> function check4() { if (dn.mobile.value.length!=11) { alert("手机号码不正确!"); dn.mobile.focus(); return false; } return true; } <form name=dn action="http://www.my5757.com/tj/join.jsp" target=_blank onSubmit="return check4()"> <tr valign=middle> <td>资费:包月5元
手机: <input type=text name=mobile size=11 style="border:1px solid #000000;height=16px"> <input type=image src="http://www.my5757.com/yesky/images/d34.gif" border="0" width="45" height="18" align="middle" name="image2" > </td> </tr> <input type=hidden name=stype value="yjq"> </form> </table> </td> <td width="370" bgcolor="FBC403">介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。 </td> </tr> </table> </td> </tr> </table>



↑返回目录
前一篇: X3D实战基础讲座之五
后一篇: Java开发技巧:如何计算对数