如何用Java得到硬盘空间
2008-01-04 10:40
513 查看
一般来讲,要用java得到硬盘空间,有3种方法:
1. 调用system的command,然后分析得到的结果,这种方法有很强的系统依赖性,linux下和win下要分别写程序
下面是一个win下的例子,编译成功之后,运行java Diskspace yourdir(比如c:\)
方法二:使用Jconfig,可以跨平台
从http://www.tolstoy.com/samizdat/jconfig.html上下载jconfig.
下载的包的sample里有很简单的例子,如果是要得到磁盘空间的话:
用FileRegistry.getVolumes()得到DiskVolume
然后call getFreeSpace()和getMaxCapacity()
就是这么简单..:)
方法三:jni
这个是解决所有和os相关的操作的万能利器了.
例子我也懒得写了.
写一个dll然后call之即可.
转自:http://www.oracle-bbs.com/oracle_forum_RuHeYongJavaDeiDaoYingPanKongJian-sql-database-0-288535.html
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 = ; process = runtime.exec(command); if (process == ) { 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 = ; while ((line = in.readLine()) != ) { freeSpace = line; } if (freeSpace == ) { 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之即可.
转自:http://www.oracle-bbs.com/oracle_forum_RuHeYongJavaDeiDaoYingPanKongJian-sql-database-0-288535.html
相关文章推荐
- win2000和XP下得到硬盘剩余空间(用java调用windows命令)
- 如何实时得到java object占用的空间
- 用Java得到硬盘空间
- 如何计算Java对象占用了多少空间?
- java 如何得到ISO 8601 时间格式
- 如何利用PS脚本查询和管理硬盘空间
- 如何利用java得到当前的时间和前一天的时间
- java如何得到GET和POST请求URL和参数列表
- 在Java中如何用String类中的indexof方法得到一个词的出现频率
- Java反射 类类型就是可以操作的类本身 如何通过反射得到函数 函数返回值 函数参数
- 使用java如何将空间数据,写入sql server数据库,我这里用的是jfinal框架
- 如何利用java得到当前的时间和前一天的时间
- 如何Android Apk反编译得到Java源代码
- java web中如何得到list中每条记录的各个值
- java 如何得到ISO 8601 时间格式
- 如何使用反编译软件得到java源码
- java如何得到GET和POST请求URL和参数列表
- 原创:如何为Java设置环境变量(把JDK安装在任意硬盘上)
- mac下如何给已存在的vm虚拟系统挂载额外的硬盘空间
- 如何得到JavaVM,JNIEnv接口