Kre*_*dns 86
只需查看File类文档即可.这是1.6中的新功能之一.
这些新方法还包括:
public long getTotalSpace()public long getFreeSpace()public long getUsableSpace()如果您仍在使用1.5,那么您可以使用Apache Commons IO库及其FileSystem类
pru*_*nge 56
Java的1.7有一个稍微不同的API,自由空间可以通过查询的FileStore通过类getTotalSpace() ,getUnallocatedSpace()和getUsableSpace()方法.
NumberFormat nf = NumberFormat.getNumberInstance();
for (Path root : FileSystems.getDefault().getRootDirectories()) {
System.out.print(root + ": ");
try {
FileStore store = Files.getFileStore(root);
System.out.println("available=" + nf.format(store.getUsableSpace())
+ ", total=" + nf.format(store.getTotalSpace()));
} catch (IOException e) {
System.out.println("error querying space: " + e.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
此API的优点是,在查询磁盘空间失败时,您会收到有意义的异常.
Jon*_*Jon 20
使用CommonsIO和FilesystemUtils:
例如
FileSystemUtils.freeSpaceKb("/");
Run Code Online (Sandbox Code Playgroud)
或者内置到JDK中:
http://java.sun.com/javase/6/docs/api/java/io/File.html#getFreeSpace()
new File("/").getFreeSpace();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74433 次 |
| 最近记录: |