[转帖]用Java得到硬盘空间_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4060 | 回复: 0   主题: [转帖]用Java得到硬盘空间        下一篇 
yd7563337
注册用户
等级:少将
经验:10191
发帖:128
精华:1
注册:2011-8-9
状态:离线
发送短消息息给yd7563337 加好友    发送短消息息给yd7563337 发消息
发表于: IP:您无权察看 2011-8-15 10:55:10 | [全部帖] [楼主帖] 楼主

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之即可.




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论