linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > JAVA >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·Ajax实现在textbox中模糊查询显
·Ajax实现分页查询
·java定时执行的三种方法
·读取数码照片中的Exif信息
·基于Spring+Hibernate+Eclip
·JAVA反编译工具jad的简单用法
·Java编程基础
·Java调用Oracle的过程和函数
·JSP获取客户端的浏览器和操作系
·java.lang包概述
·Java中利用通讯API编写短信软件
·利用Java实现网页浏览器
·Java文件操作大全
·JAVA技术:上传图片的缩放处理
·基于JSF和DAO模式的大型设备采
最新文章
·实例解析:Linux操作系统下Jav
·JAVA得到网卡物理地址(Windows
·Ajax实现在textbox中模糊查询显
·Ajax简单示例之改变下拉框动态
·AJAX如何与后台交互
·Ajax实现分页查询
·Ajax核心:XMLHTTP组件相关技术
·面向Java程序员的Ajax:构建动态
·JSP获取客户端的浏览器和操作系
·提高Java技能的几种简单有效的
·敏捷开发中需掌握移除重复代码
·Java中利用通讯API编写短信软件
·关于String和StringBuffer
·用Java得到硬盘空间
·如何优化JAVA程序设计和编码,
Google
 
用Java得到硬盘空间
[ 作者:  加入时间:2007-10-25 13:49:49  来自:51CTO ]

用Java得到硬盘空间,有3种方法:

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

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])); 
} 
} 
} 
} 

2、使用Jconfig,可以跨平台

从http://www.tolstoy.com/samizdat/jconfig.html上下载jconfiguzJLinux联盟
下载的包的sample里有很简单的例子,如果是要得到磁盘空间的话:uzJLinux联盟
用FileReGIStry.getVolumes()得到DiskVolume uzJLinux联盟
然后call getFreeSpace()和getMaxCapacity() uzJLinux联盟
就是这么简单:)

3、jni

这个是解决所有和os相关的操作的万能利器了。uzJLinux联盟
例子写一个dll然后call之即可。

Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·通过JAVAScript实现页面自适应  (2007-10-30 14:27:43)
 ·如何优化JavaScript脚本的性能  (2007-10-30 14:25:25)
 ·几个有用的Javascript验证脚本  (2007-10-30 14:24:47)
 ·Javascript+ASP打造无刷新新闻列表  (2007-10-30 14:23:38)
 ·如何用javascript防止双击  (2007-10-30 14:19:44)
 ·JavaScript 访问 JSF 组件的方法  (2007-10-30 14:18:21)
 ·JavaScript实现仿Windows关机效果  (2007-10-30 14:14:42)
 ·JavaScript去除空格的三种方法  (2007-10-30 14:07:32)
 ·用Javascript评估用户输入密码的强度  (2007-10-30 14:06:19)
 ·JavaScript处理事件:单击事件onClick  (2007-10-30 14:01:38)