如何用java得到硬盘空间

2016-02-19 16:26 3 1 收藏

下面是个简单易学的如何用java得到硬盘空间教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - 编程语言 】

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  用FileRegistry.getVolumes()得到DiskVolume

  然后call getFreeSpace()和getMaxCapacity()

  就是这么简单..:)

  方法三:jni

  这个是解决所有和os相关的操作的万能利器了.

  例子我也懒得写了.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  写一个dll然后call之即可.

来源:https://www.tulaoshi.com/n/20160219/1612118.html

延伸阅读
如何为Win7系统释放更多硬盘空间   空间对于电脑来说是多么的重要,富足的空间可以让我们操作起来更畅快,运行速度也更快速了。但是电脑用久了,存在不少缓存文件,导致win7电脑磁盘空间不足,各种问题就应运而生,那该怎么解决呢? 1.打开我的电脑文件夹选项 查看显示所有文件和文件夹,把隐藏受保护的系统文件勾去掉,确定。 ...
标签: 电脑入门
方法一、清空回收站 在之前的微软系统中,我们最简单的删除方式就是把文件丢在回收站里,新版也不例外,你只要清空回收站,就把空间可以很快的找回来。 在回收站图标上右键点击,能够直接清空回收站里的所有内容。什么?电脑桌面上没有回收站图标,开始菜单 设置 系统 存储,并在此电脑中选择临时文件,然后点击清空回收站,效果一样...
标签: 电脑入门
电脑用久了就知道,我们系统分区的空间只会越用越少。分析其原因,一方面是因为软件越装越多,占去了大量空间,另一方面是因为系统和软件在使用过程中产生临时文件没有及时清理,越积越多。目前有很多专业的系统清理工具,不过这些软件大多都忽略了QQ生成的临时文件。 也许大家会觉得奇怪,QQ就算有临时文件,无非就是些QQ秀图片、自...
Windows 7智能创建还原点 节约硬盘空间   在Windows XP时代,用户可以通过Ghost方式解决备份还原。但当操作系统进入Windows 7时代,使用Ghost方式备份还原,显然还不如系统自带的还原功能好。问题是,在Windows 7中启用系统还原后,它会自动创建还原点(默认周期为一天)。还原点越多,也就意味着硬盘空间将会越来越小。 由此,...
标签: 电脑入门
图老师小编无意中发现自己的电脑上E盘的容量缩水了好多,将近两个G了,但图老师小编发现E盘属性和E盘所有文件(已包含所有隐藏文件)属性二者显示的大小不一致。这到底是啥情况呢? ▲E盘所有文件属性 ▲E盘属性 因E盘并非系统所在分区,除了回收站(回收站已清空)是系统文件夹之外,其他文件都是自己的个人文件,个人文件大小肯定不会有...

经验教程

928

收藏

21
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部