网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
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,移动开发
本月文章推荐
.DiskSuite做raid 0的note.
.Java套接字编程(下)(1).
.Eclipse 平台入门.
.深入浅出基于Java的建造设计模式.
.网络应用程序支持中文的简单试验.
.Sun Java技术认证风靡全球.
.J2EE Web服务客户端质量报告(五).
.Spring Singleton的陷阱.
.玩转Java的CLASSPATH(四)总结.
.什么是XSP?.
.Java 理论与实践:线程池与工作队.
.Java Servlet 编程及应用.
.J2EE1.4中的Servlet部署应用.
.Jini.
.全面介绍Xen虚拟机 深入学习Xen新.
.Tomcat+Mysql入门实例:滚动横幅广.
.设计模式在EJB中的应用.
.JAVA初学常见问题.
.Java 线程综合述.
.编写一个基于Java Robot类的屏幕.

如何用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深度分析与比较 人气:658
下一篇:如何在Java中实现Job Scheduling 人气:723
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐