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里有很简单的例子,如果是要得到磁盘空间的话:

用FileRe

GIS

try.getVolumes()得到DiskVolume 然后call getFreeSpace()和getMaxCapacity() 就是这么简单..:) 方法三:jni 这个是解决所有和os相关的操作的万能利器了. 例子我也懒得写了. 写一个dll然后call之即可.

java 程序硬盘,用Java得到硬盘空间相关推荐

  1. matlab调用Java程序时出现 Java.lang.OutOfMemoryErrot: GC overhead limit exceeded

    matlab调用Java程序时出现 java.lang.OutOfMemoryError: GC overhead limit exceeded JDK1.6.0_37和JDK_1.7.0_60版本, ...

  2. java跟python对比_【多年的Java程序员总结Java与Python的对比 】

    多年的Java程序员总结Java与Python的对比 作为一个有多年职场经验的技术员,每当我遇到同行时,我都会问一句"你现在用的是什么语言?你喜欢用哪种语言?"至于答案,容后再说. ...

  3. mysql连接java程序_实现Java连接mysql

    Java连接mysql数据库,代码经过运行准确无误. 下面为实例----> 用数据库操纵工具(例: SQLyogEnt)操纵mysql建表,或dos下建,如下: 数据库名:scutcs 表名:s ...

  4. Java程序设计基础——简单Java程序

    简单Java程序 一.Java应用程序 1.HelloWorldApp 源代码: 编译运行: 二.Java小应用程序 1.HelloApplet 源代码: 编译运行: 2.firstApplet 源代 ...

  5. 越来越多的Java程序员转行Java大数据...

    JAVA的精密,强大,拥有其它语言不可替代的性能和可维护性,早已经是成为最受欢迎的编程语言之一,很多人想进入IT行业,首选的第一门语言就是JAVA. 但是,在未来肯定是大数据的天下,人工智能的爆发,将 ...

  6. 你好,世界:第一个Java程序(《Java与Python学习通法》)

    大家好,我是 jacky 朱元禄,本小节jacky将跟大家分享第一个 java 程序,根据第一程序我们来总结一下 Java 程序的运行机制: 前面我们已经搭建好了 Java 的开发环境,那我们应该如何 ...

  7. 在Java程序中打印java运行时参数

    在Java程序中打印java运行时参数 本文是基于CentOS 7.3系统环境,进行java的学习和使用 CentOS 7.3 1. java运行时参数 Java程序在运行时,主要分为两大类参数,一类 ...

  8. 写给Java程序员的Java虚拟机学习指南

    大家好,我是极客时间<深入拆解Java虚拟机>作者.Oracle Labs高级研究员郑雨迪.有幸借这个专题的机会,能和大家分享为何Java工程师要学Java虚拟机?如何掌握Java虚拟机? ...

  9. hannoi塔java程序_基于Java实现的Hannoi汉诺塔自动演示程序

    1 设计要求 设计GUI界面的Hannoi塔.汉诺塔中有三个座,名字分别为A,B,C.初始状态时A塔上有3个大小不等的盘子,这些盘子从座底到座顶按着大小顺序依次摆放在A座上.用户可以用鼠标选中盘子,然 ...

  10. java程序崩溃查询,java – 有程序识别它上次崩溃了吗?

    Java程序崩溃有三个原因: >未处理的RuntimeException.这可以通过main中的try-catch轻松解决. >未处理的错误.这些是罕见的,但也可以主要捕获.我通常主要抓住 ...

最新文章

  1. 微信小程序一些常见的坑
  2. 个人工作总结10(第二阶段)
  3. 记一次使用 android 自带 WebView 做富文本编辑器之API、机型的兼容及各种奇葩bug的解决...
  4. 给Python初学者的最好练手项目,进来看就对了!
  5. 从菜鸟成为数据科学家的养成方案
  6. python笔记之变量(variable)
  7. oracle中OEM证书失效怎么办,安全证书过期怎么办 网站安全证书失效处理【解决方法】...
  8. array函数参数 scala_scala – 在Spark SQL中将数组作为UDF参数传递
  9. zabbix_get查看agent端的key值提示没有权限:Permission denied
  10. 店铺淘宝双十一活动利润的快速量化。
  11. Vivado2017.4软件安装
  12. 『WPF』DataGrid的使用
  13. B2 Spice A/D v4.1.2.0 Pro
  14. google code prettify 多款主题颜色及下载
  15. Android Xml解析
  16. 使用MapReduce实现专利文件的分析
  17. 2017报计算机热不热,【2017年10月份热吗】2017年10月份热不热_亲亲宝贝网
  18. 什么是Web2.0(图解Web2.0)
  19. doc文件转换为docx文件
  20. 适合零基础编程学员学习的网站,APP

热门文章

  1. JAVA中的静态成员
  2. 中国大数据人才薪酬报告
  3. android 从媒体库去数据
  4. js中top,left,height,width相关参数的解析和实例
  5. 6美元进公园随便挖钻石
  6. C++ 解引用操作符重载
  7. yum配合rpm查看软件包安装位置
  8. linux时间同步ntp服务的安装与配置
  9. UVa 12657 双向链表
  10. MinGW: TOO MANY SECTIONS issue