如何获得可用的硬盘空间?
来源:互联网 发布:javascript 正则 转义 编辑:程序博客网 时间:2024/06/04 18:56
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>目前尚未发现有任何干净利落的纯Java方法能够解决这个问题。通常的解决方案是直接访问操作系统获得这些信息。有一个被称为JConfig的类库提供了一些方法可以获得磁盘和文件信息,但是可以肯定这个类库使用了JNI方法。 下载地址:http://www.tolstoy.com/samizdat/jconfig.html 如果你使用的是晕倒死系列操作系统,那么下面的方法也许能够获得正确的结果。我之所以说也许,是因为我在多个晕倒死平台上作过实际测试,在英文版的晕倒死上基本上都能够得到正确的结果,在中文版的晕倒死上基本上都不能够获得正确的结果。
String osname = System.getProperty("os.name"); String command = ""; if (osname.indexOf("NT") > -1) command = "c://winnt//System32//cmd.exe"; else if (osname.indexOf("Windows") > -1) command = "c://windows//command.com"; Process p = Runtime.getRuntime().exec( command " /c dir > c://dir.txt"); p.waitFor();
然后你需要做的是对得到的dir.txt文件进行分析。 如果你使用的是UNIX/Linux操作系统,你可以使用类似的方法来获得相关信息。建议使用的命令是df -k > dir.txt。