- 浏览: 198077 次
- 性别:
- 来自: 北京
-
最新评论
-
zeroliu:
测试评论。。。
UMP编程比赛:封装TCP连接池和DAL客户端接口库。(7.27更新) -
zeroliu:
新的代码,有一个平滑压缩的处理,还有变灰的处理。
impor ...
Java小程序:批量图片处理(缩小和添加Logo) -
小冰糕:
kenter1643 写道前几天我也在弄串口通讯,也是得到空的 ...
学习笔记:Java串口编程(短信Modem). -
salever:
不错,
学习笔记:Java串口编程(短信Modem). -
zeroliu:
我发现本帖,在blog栏目已经找不到了,搜索也不行,不知道什么 ...
反编译android应用,降低权限反吸费和隐藏广告,重新打包和签名【原创】
原来发布在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代码及测试。
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
这些import内容在Eclipse中是可以自动导入的,一般源代码为了精简,都不需要含这些内容。
这已经是完整的代码,main()中就是测试用例;至于UtilLog仅仅是配置log4j日志而已,可以不用,把log4j.properites直接放在运行路径就可,或者用System.out.println替代。
保持和BlogDriver的一致
【虎.无名】最近封装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如下:
本文主要演示了一种通用方式,区分win和linux等操作系统,调用外部进程来获得结果。
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包就可以
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
老虎头像好可爱

发表评论
-
反编译android应用,降低权限反吸费和隐藏广告,重新打包和签名【原创】
2011-07-27 13:20 8756功能:反编译apk降低权限及重新签名 场景:很多软件,申请了一 ... -
修复mina2客户端IoSession.close()在jdk1.5下关闭不彻底问题
2009-11-23 15:51 6176原帖:http://hi.baidu.com/zeorliu/ ... -
日志API改进:用commons-log还是slf4j?这是一个问题!
2009-02-11 10:23 3700用commons-log还是slf4j?这是一个问题! 看jd ... -
在Web中动态生成验证码:Servlet和Rest模式
2008-09-24 10:23 3594【虎.无名】登录处理常用到一个生成随机校验码图片的处理。下面是 ... -
【虎.无名】自定义Java的REST行为分发器
2008-09-10 09:10 3047【虎.无名】在Restlet和Rails中,资源所支持的Act ... -
算法分析:货郎担问题求解分析
2008-07-22 10:28 4684【虎.无名】看到一个帖子 【迷题:走遍全国各省会的最短路线问题 ... -
答复: 谁能告诉我,什么是企业“级”应用?
2008-04-24 16:16 2261http://www.iteye.com/topic/1078 ... -
答greatolee:OMADRM的agent并不能获取明文内容
2008-04-23 17:00 2020greatolee,您好! OMADRM有2个版本,你说的是O ... -
答g_ktcy短信格式:文本,超长文本,WapPush、OMA-DRM权限、Wap书签、GPRS配置
2008-04-17 15:57 3216g_ktcy,您好! 短信有很多种类:文本,超长文本,WapP ... -
Linux下通过ftp命令实现断点续传(reget)
2008-02-19 14:43 13750同事在Linux通过FTP获取一个1.3G的大文件,传了一个上 ... -
学习笔记:Java串口编程(短信Modem).
2007-08-03 12:04 18918最终目标:在Linux下提供一个稳定可靠的Java短信发送服务 ... -
Symbian OS C++学习笔记2异常退出
2007-03-21 09:40 3262Ch2. 异常退出(Leave) -------------- ... -
OMADRM2学习笔记:DCF-v2结构解析
2007-03-21 09:39 2882【虎.无名】由于需要支持流媒体格式,因而OMA-DRM-DCF ... -
Symbian OS C++学习笔记1命名约定
2007-03-09 15:52 2384http://zeroliu.blogdriver.com/z ... -
OMADRM2学习笔记DCF-v2规范定义
2007-03-09 15:51 3672http://zeroliu.blogdriver.com/z ... -
用Java动态代理实现委托模式
2006-10-23 17:35 5431委托模式是软件设计模式中的一项基本技巧。在委托模式中,有两个对 ... -
Java开发八荣八耻 zt
2006-10-20 09:54 1756以动手实践为荣,以只看不练为耻。 以打印日志为荣,以出错不报为 ...
相关推荐
"分区大小"则表明程序还支持查看各个磁盘分区的详细情况,包括每个分区的大小、剩余空间和使用率,这对于管理和优化磁盘分区布局至关重要。 在压缩包子文件的列表中,"Source"可能包含了程序的源代码文件,这些文件...
然而,对于开发者来说,通常会使用编程语言提供的API或者库来实现更高级的功能,例如动态监控磁盘容量变化、动态报告剩余空间等。例如,在Python中,可以使用`psutil`库,Java中则有`java.io.File`类,C++中可以使用...
2. **文件系统统计信息**:包括文件系统的总容量、已用空间、剩余空间、文件和目录数量、I/O 操作的性能指标等,这些信息对于监控系统健康状况和优化资源分配至关重要。 3. **Java 语言**:Java 具有平台无关性,...
### 数据结构与算法Java中文知识点概述 #### 一、Java与面向对象程序设计 ##### 1.1 Java语言基础知识 **1.1.1 基本数据类型及运算** - **基本数据类型**:Java提供了八种基本数据类型,包括四种整型(byte、...
- 使用`df -h`命令查看文件系统的总容量、已用空间和剩余空间。 - 使用`du -sh *`或`du -ah`命令查看子目录的大小。 - **Windows环境下**: - 使用`dir`命令结合`/s`参数查看子目录的大小。 #### 4. SGA定义及其...
5. **错误处理**:在分割过程中,可能遇到如文件不存在、磁盘空间不足等问题,程序需要有良好的错误处理机制,以便在出现问题时给出明确的错误提示。 6. **文件命名**:分割后的子文件通常按照一定的规则命名,例如...
### JAVA与数据结构 #### Java语言基础知识 - **基本数据类型及运算** - Java支持多种基本数据类型,包括整型(如`byte`, `short`, `int`, `long`)、浮点型(如`float`, `double`)、字符型(`char`)以及布尔型...
- **Java 编译器**:用于将 Java 源代码编译成字节码。 - **Java 运行工具**:用于运行 Java 应用程序。 - **Java 文档生成工具**:用于从源代码注释中生成文档。 - **Java 打包工具**:用于将 Java 应用程序...