java1.6 获取磁盘大小,在Java中查找总磁盘大小的可移植方法(Java …

java1.6 获取磁盘大小,在Java中查找总磁盘大小的可移植方法(Java …更新:对于错误理解此问题,我深表歉意,建议您复制FileSystemUtils方法,但是修改它运行的命令.在dos中,您可以使用fsutil命令获取可用字节和总计字节:fsutilvolumediskfree[driveletter]在我的盒子上,得到以下结果:Total#offreebytes:41707524096Total#ofbytes…

java1.6 获取磁盘大小,在Java中查找总磁盘大小的可移植方法(Java ...

更新:

对于错误理解此问题,我深表歉意,建议您复制FileSystemUtils方法,但是修改它运行的命令.

在dos中,您可以使用fsutil命令获取可用字节和总计字节:

fsutil volume diskfree [drive letter]

在我的盒子上,得到以下结果:

Total # of free bytes : 41707524096

Total # of bytes : 80023715840

Total # of avail free bytes : 41707524096

在Unix上,命令仍然是“ df -k”,您只对“ Free”左侧的“ 1024-blocks”列感兴趣(例如,来自以下Wikipedia的示例).您显然需要将结果乘以1024.

Filesystem 1024-blocks Free %Used Iused %Iused Mounted on

/dev/hd4 32768 16016 52% 2271 14% /

/dev/hd2 4587520 1889420 59% 37791 4% /usr

/dev/hd9var 65536 12032 82% 518 4% /var

/dev/hd3 819200 637832 23% 1829 1% /tmp

/dev/hd1 524288 395848 25% 421 1% /home

/proc – – – – – /proc

/dev/hd10opt 65536 26004 61% 654 4% /opt

假设您复制FileSystemUtils来实现“ totalSpaceKB()”,以委派给等效的特定于OS的方法. Windows的实现将是这样的(请注意使用“查找”来修剪fsutil的输出以仅获得总大小):

long totalSpaceWindows(String path) throws IOException {

path = FilenameUtils.normalize(path);

if (path.length() > 2 && path.charAt(1) == ‘:’) {

path = path.substring(0, 2); // seems to make it work

}

// build and run the ‘fsutil’ command

String[] cmdAttribs = new String[] {

“cmd.exe”,

“/C”,

“fsutil volume diskfree ” + path

+ ” | Find \”Total # of bytes\”” };

// read in the output of the command to an ArrayList

List lines = performCommand(cmdAttribs, Integer.MAX_VALUE);

//because we used Find, we expect the first line to be “Total # of bytes”,

//you might want to add some checking to be sure

if (lines.size() > 0) {

String line = (String) lines.get(0);

String bytes = line.split(“:”)[1].trim();

return Long.parseLong(bytes);

}

// all lines are blank

throw new IOException(

“Command line ‘fsutil volume diskfree’ did not return

+ “any info for path ‘” + path + “‘”);

}

Unix的实现与freeSpaceUnix()相同,但是在方法末尾删除了对tok.nextToken()的两个调用.

/** comment these two lines so the token received is the total size */

tok.nextToken(); // Ignore 1K-blocks

tok.nextToken(); // Ignore Used

/** this will now be the total size */

String freeSpace = tok.nextToken();

return parseBytes(freeSpace, path);

}

其他平台的实现将类似.

希望这对误读问题有所帮助,并为此道歉.

原始答案(获取可用字节,而非总计).

在Java 6之前,还没有一种完美的方法(请参见bug).建议不要使用自己的库,而建议使用库来为您执行特定于平台的处理.

Apache commons-io的类型为FileSystemUtils,提供了静态方法freeSpaceKb().它适用于Windows和某些Unix实现(请参见下面Javadoc的引文)

从Javadoc:

public static long freeSpaceKb(String path)

throws IOException

Returns the free space on a drive or volume in kilobytes by invoking the command line.

FileSystemUtils.freeSpaceKb(“C:”); // Windows

FileSystemUtils.freeSpaceKb(“/volume”); // *nix

The free space is calculated via the command line. It uses ‘dir /-c’ on Windows, ‘df -kP’ on AIX/HP-UX and ‘df -k’ on other Unix.

In order to work, you must be running Windows, or have a implementation of Unix df that supports GNU format when passed -k (or -kP). If you are going to rely on this code, please check that it works on your OS by running some simple tests to compare the command line with the output from this class. If your operating system isn’t supported, please raise a JIRA call detailing the exact result from df -k and as much other detail as possible, thanks.

今天的文章java1.6 获取磁盘大小,在Java中查找总磁盘大小的可移植方法(Java …分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31964.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注