网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  用java得到硬盘空间的三种不同方法     
  文章作者:未知  文章来源:水木森林  
  查看:124次  录入:管理员--2007-11-17  
 
  一般来讲,要用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]));
  }
  }
  }
  }
  
  方法二:使用jconfig,可以跨平台
  
  从http://www.tolstoy.com/samizdat/jconfig.html上下载jconfig.
  
  下载的包的sample里有很简单的例子,如果是要得到磁盘空间的话:
  
  用fileregistry.getvolumes()得到diskvolume
  
  然后call getfreespace()和getmaxcapacity()
  
  就是这么简单..:)
  
  方法三:jni
  
  这个是解决所有和os相关操作的万能利器了.
  
  例子我也懒得写了.
  
  写一个dll然后call之即可.
 
 
上一篇: 如何在java中编程实现数字签名系统    下一篇: 使用 java 1.2 的 authenticator 类
  相关文档
一个jsp连接mysql的简单例子 11-17
一些非常有用的java常用方法( 2) 11-17
rss 2.0 specification 11-17
使用模仿对象进行单元测试 11-17
buffalo ajax/amowa framework发布1.2版 11-17
正确的解决用户退出问题?djsp和struts 11-16
java 继承的一个实例 11-17
java中最为关键的几个知识点 11-16
sso各产品单点登录的简单实现 11-17
文件锁 11-17
canvas中使用font 11-17
一个虽然复杂但可直接套用的线程池实例 01-07
jbuilder2005单元测试之业务类介绍 11-17
protected:“友好的一种” 11-17
bea的workshop studio开发者的ide利器 11-17
javascript实例教程(15) 日期函数 11-16
j2se5.0用executor灵活处理事件下发 11-17
java访问com组件_jacob使用指南 11-17
试试看把xml转成pdf的有效工具:fop 11-17
从ifelse到设计模式的转变 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息