`
zeroliu
  • 浏览: 192986 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

查看磁盘剩余空间:Java代码改进

阅读更多
原来发布在Blog上的:http://zeroliu.blogdriver.com/zeroliu/1221778.html

【虎.无名】最近封装JMX的MBean,有一个监控磁盘空间的需求。在网上找遍了,列出了3种方法,第1种只能windows系统,第2种就不用说了,需要一个扩展库。至于用JNI则就没必要了。最新的jdk6.0有相应的方法,其它版本还没有,研究了一下方法一和方法二,主要原理就是:通过java中的Process类来调用外部命令,如dir、ls、df -k、du等,然后捕获其标准输出,从而获取所需数据。具体代码如下。。。

方法一:执行外部命令dir/df,然后捕获输出,分析输出获取所需数据。
原始代码地址:http://www.jr163.org/cup2/36/36934.htm
【虎.无名】原始代码存在缺陷,未处理win xp,而且不支持unix等操作系统;经过修改后的代码见附录。

方法二:使用Jconfig,可以跨平台
  从http://www.tolstoy.com/samizdat/jconfig.html上下载jconfig。
  下载的包的sample里有很简单的例子,如果是要得到磁盘空间的话:
  用FileRegistry.getVolumes()得到DiskVolume。
  然后call getFreeSpace()和getMaxCapacity()。
  就是这么简单。

方法三:使用jni技术 【虎.无名】针对需求而言太复杂,也无法移植,不建议使用。
  这个是解决所有和os相关的操作的万能利器了。
  例子我也懒得写了。
  写一个dll然后call之即可。

http://tolstoy.com/samizdat/jconfig.html

What is it?
JConfig is a cross-platform library that supplements the core Java API and solves many programming tasks.

It lets you work with files, web browsers, processes, file types, and other system-level items in a much more advanced manner than that provided by the standard Java class libraries. For instance, you can use it to launch web browsers or other external applications instead of using Runtime.exec or solutions that only work on one platform.

JConfig is similar to some of the Microsoft extensions to Java, except: it's completely cross-platform! JConfig runs on Windows, Mac, Unix, and other platforms to come.

The download even includes the complete source code!

See how JConfig compares with the standard Java API.

What can I do with it?
Here's a partial list, by category:

Files: Enumerate the user's disk drives, and obtain extended information on files, directories, volumes, and filesystems: their icons, creation dates, version information, mount points, and much more...
Web Browsers: Launch a file or URL in the user's Web browser...
Video Monitors: Enumerate and get information on the user's video monitors: bit depth, bounds, etc...
External Processes: Create external processes, send basic commands to external processes, obtain the PSN or HWND of a process you created, and enumerate the currently running processes...
File Types: Find applications associated with a given file type, find applications by name, and convert between Windows file extensions and Mac creator/file type codes...
--------

【虎.无名】修改后的DiskSpace代码及测试。

public class DiskSpace {
 public static final String CRLF = System.getProperty("line.separator");
 public static final int OS_Unknown  = 0;
 public static final int OS_WinNT  = 1;
 public static final int OS_Win9x  = 2;
 public static final int OS_Linux  = 3;
 public static final int OS_Unix  = 4;
 
 private static Log   _log = LogFactory.getLog(DiskSpace.class);
 private static String  _os = System.getProperty("os.name");
 protected static String os_exec(String[] cmds) {
  int   ret = 0;
  Process  porc = null;
  InputStream perr = null, pin = null; 
  StringBuffer sb = new StringBuffer();
  String  line = null;
  BufferedReader br = null;
  try {
  // for(int i=0; i   porc = Runtime.getRuntime().exec(cmds);//执行编译操作
  // porc = Runtime.getRuntime().exec(cmds, null, null);
   perr = porc.getErrorStream();
   pin  = porc.getInputStream();
   //获取屏幕输出显示
   //while((c=pin.read())!=-1) sb.append((char) c);
   br = new BufferedReader(new InputStreamReader(pin));
   while((line=br.readLine())!=null) {
   // System.out.println("exec()O: "+line);
    sb.append(line).append(CRLF);
   }
   //获取错误输出显示
   br = new BufferedReader(new InputStreamReader(perr));
   while((line=br.readLine())!=null) {
    System.err.println("exec()E: "+line);
   }
   porc.waitFor();   //等待编译完成   
   ret = porc.exitValue(); //检查javac错误代码
   if (ret!=0) {
    _log.warn("porc.exitValue() = "+ret);
   }   
  }catch(Exception e) {
   _log.warn("exec() "+e, e);
  }finally {
   porc.destroy();
  }
  return sb.toString();
 }
 
 protected static int os_type() {
        //_log.debug("os.name = "+os); //Windows XP
        String os = _os.toUpperCase();
        if (os.startsWith("WINDOWS")) {
         if (os.endsWith("NT") || os.endsWith("2000") || os.endsWith("XP"))
          return OS_WinNT;
         else return OS_Win9x;
        }else if (os.indexOf("LINUX")>0) return OS_Linux;
        else if (os.indexOf("UX")>0)   return OS_Unix;
        else           return OS_Unknown;
  }
 protected static long os_freesize(String dirName) {
  String[] cmds = null;
  long freeSize = -1;
  int osType = os_type();
  switch(osType) {
  case OS_WinNT: 
   cmds = new String[]{"cmd.exe", "/c", "dir", dirName};
   freeSize = os_freesize_win(os_exec(cmds));
   break;
  case OS_Win9x: 
   cmds = new String[]{"command.exe", "/c", "dir", dirName};
   freeSize = os_freesize_win(os_exec(cmds));
   break;
  case OS_Linux:
  case OS_Unix:
   cmds = new String[]{"df", dirName};
   freeSize = os_freesize_unix(os_exec(cmds));
   break;
  default:
  }
  return freeSize;
 }
 protected static String[] os_split(String s) {
//  _log.debug("os_split() "+s);
  String[] ss = s.split(" "); //空格分隔;
  List ssl = new ArrayList(16);
  for(int i=0; i   if (ss[i]==null)  continue;
   ss[i] = ss[i].trim();
   if (ss[i].length()==0) continue;
   ssl.add(ss[i]);
//   _log.debug("os_split() "+ss[i]);
  }
  String[] ss2 = new String[ssl.size()];
  ssl.toArray(ss2);
  return ss2;
 }
 private static long os_freesize_unix(String s) {
  String lastLine = os_lastline(s); //获取最后一航;
  if (lastLine == null) {
         _log.warn("(lastLine == null)"); return -1;
        }else lastLine = lastLine.trim();
  //格式:/dev/sda1    101086     12485     83382  14% /boot
  //lastLine = lastLine.replace('\t', ' ');
  String[] items = os_split(lastLine);
  _log.debug("os_freesize_unix() 目录:\t"+items[0]);
  _log.debug("os_freesize_unix() 总共:\t"+items[1]);
  _log.debug("os_freesize_unix() 已用:\t"+items[2]);
  _log.debug("os_freesize_unix() 可用:\t"+items[3]);
  _log.debug("os_freesize_unix() 可用%:\t"+items[4]);
  _log.debug("os_freesize_unix() 挂接:\t"+items[5]);
  if(items[3]==null) {
   _log.warn("(ss[3]==null)");   return -1;
  }
  return Long.parseLong(items[3])*1024; //按字节算
 }
 private static long os_freesize_win(String s) {
  String lastLine = os_lastline(s); //获取最后一航;
  if (lastLine == null) {
         _log.warn("(lastLine == null)");
            return -1;
        }else lastLine = lastLine.trim().replaceAll(",", "");
  //分析  
  String items[] = os_split(lastLine); //15 个目录  1,649,696,768 可用字节
  if (items.length<4) { _log.warn("DIR result error: "+lastLine); return -1;}
  if (items[2]==null) { _log.warn("DIR result error: "+lastLine); return -1;}
        long bytes = Long.parseLong(items[2]); //1,649,696,768 
        return bytes;
 }
 protected static String os_lastline(String s) {
  //获取多行输出的最后一行;
  BufferedReader br = new BufferedReader(new StringReader(s));
  String line = null, lastLine=null;
  try {
   while((line=br.readLine())!=null) lastLine = line;
  }catch(Exception e) {
   _log.warn("parseFreeSpace4Win() "+e);
  }
  //_log.debug("os_lastline() = "+lastLine);
  return lastLine;  
 }
// private static String os_exec_df_mock() { //模拟df返回数据
//  StringBuffer sb = new StringBuffer();
//  sb.append("Filesystem     1K-块        已用     可用 已用% 挂载点");
//  sb.append(CRLF);
//  sb.append("/dev/sda1    101086     12485     83382  14% /boot");
//  sb.append(CRLF);
//  return sb.toString();
// }
    public static long getFreeDiskSpace(String dirName) {
        //return os_freesize_unix(os_exec_df_mock()); //测试Linux
  return os_freesize(dirName);//自动识别操作系统,自动处理
    }
    public static void main(String[] args) throws IOException {
     UtilLog.configureClassPath("resources/log4j.properties", false);
     args = new String[3]; int x=0;
     args[x++] = "C:";     args[x++] = "D:"; args[x++] = "E:";
        if (args.length == 0){
            for (char c = 'A'; c <= 'Z'; c++) {
                String dirName = c + ":\\";  //C:\ C:
                _log.info(dirName + " " +
                getFreeDiskSpace(dirName));
            }
        }else{
            for (int i = 0; i < args.length; i++) {
             _log.info(args[i] + " 剩余空间(B):" + getFreeDiskSpace(args[i]));
            }
        }
    }
}


分享到:
评论
9 楼 yinger_fei 2010-10-15  
想问一下,这个查看linux磁盘空间大小的方法可以用么?
8 楼 zeroliu 2008-09-10  
完整的import如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.bs2.core.UtilLog;//只是为了在main中指定log4j配置,可不用。

本文主要演示了一种通用方式,区分win和linux等操作系统,调用外部进程来获得结果。
7 楼 siriuscor 2008-08-02  
apache标准库中有一个函数
org.apache.commons.io.FileSystemUtils.freeSpaceKb("C:\\");
导入commons-io包就可以
6 楼 zeroliu 2006-10-23  
引用
可是LogFactory没定义,这是apache中的一个类,怎么引用到其中呢?

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
这些import内容在Eclipse中是可以自动导入的,一般源代码为了精简,都不需要含这些内容。
5 楼 zqyxq007 2006-10-23  
可是LogFactory没定义,这是apache中的一个类,怎么引用到其中呢?
4 楼 zeroliu 2006-10-23  
引用
你好,看到这段代码非常高兴,但有些地方不明白,能否提供完整的源代码,谢谢!邮箱:zqyxq007@163.com

这已经是完整的代码,main()中就是测试用例;至于UtilLog仅仅是配置log4j日志而已,可以不用,把log4j.properites直接放在运行路径就可,或者用System.out.println替代。
3 楼 zqyxq007 2006-10-23  
你好,看到这段代码非常高兴,但有些地方不明白,能否提供完整的源代码,谢谢!邮箱:zqyxq007@163.com
2 楼 zeroliu 2006-10-09  
引用
老虎头像好可爱

保持和BlogDriver的一致
1 楼 ouspec 2006-10-09  
老虎头像好可爱

相关推荐

Global site tag (gtag.js) - Google Analytics