动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
电脑书籍下载:程序设计书籍 | 数据库教程书籍 | 平面与多媒体书籍 | 网络通讯书籍 | 系统管理书籍 | 网络安全书籍 | 认证考试书籍
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息



本月文章推荐
.Java Socket编程(一)Socket传输.
.java开源项目研究 1. 引子.
.Java咖啡馆(4)——品味第一杯咖啡.
.instanceof和回调概念.
.Think in java 3rd 中文版12_13.
.Java开发者XML基础全程详细讲解.
.20分钟熟悉猛虎脾气-JDK1.5新特.
.浅谈实际开发中数据源在JDBC中的.
.分析Java中乱码问题产生的根源.
.如何在J2ME的低级界面中轻松实现.
.java中删除数据库中重复数据的几.
..NET真的战胜了J2EE吗?它能吗?.
.Sun电信行业解决方案.
.用 Spring MVC 轻松进行应用程序.
.使用 Java Reflection.
.Java对象序列化(整理篇).
.UDDI4J v2 编程.
.无需JCE用底层API实现开发RSA.
.Java中的违例自变量.
.sort 方法.

如何用Java得到硬盘空间

发表日期:2008-1-5 |



  一般来讲,要用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 Collections---HashMap深度分析与比较 人气:301
下一篇:如何在Java中实现Job Scheduling 人气:390
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-7-6 飞天论坛FTBBS ASP v6.3 Build 0
2008-7-6 飞天论坛FTBBS ASP v6.3 Build 0
2008-7-6 飞天论坛FTBBS ASP v6.8 Build 0
2008-7-6 讯息内容管理系统 v2.1
2008-7-6 三五电影程序 v2.0
2008-7-6 神鹰腾讯小说小偷 v3.0
2008-7-6 EasyIDE Framework v1.0 Build 2
2008-7-6 品告CMS系统(电影版) v0.9
2008-7-6 QQ自动登录器 C# 源码 v1.0
2008-7-5 AgileMessenger即时通讯工具 v1.
2008-7-5 GoodCalculator2.0版固件计算器
2008-7-5 RepoName源地址搜索工具 v1.21b
2008-7-5 AgileMessenger即时通讯工具 v1.
2008-7-5 TouchCopy多媒体管理软件 v3.13完
2008-7-5 VideosTone视频铃声 v1.1汉化破解
2008-7-5 TouchPad触摸板 v4.44破解版
2008-7-5 VideosTone破解补丁 v1.0
2008-7-5 Feeds GoogleReader客户端 v0.4.3


  发表评论
姓 名: 验证码:
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報