java 得到磁盘的可用空间
来源:互联网 发布:数据库查询语句select 编辑:程序博客网 时间:2024/04/27 20:10
import java.util.*;
import java.io.*;
public class DiskUtil {
public static long getFreeSpace(String path) throws Exception {
return getFreeSpace(path, "GBK");
}
public static long getFreeSpace(String path, String charset) throws Exception {
if (System.getProperty("os.name").startsWith("Windows")) {
return getFreeSpaceOnWindows(path, charset);
}
if (System.getProperty("os.name").startsWith("Linux")) {
return getFreeSpaceOnLinux(path);
}
throw new UnsupportedOperationException(
"The method getFreeSpace(String path) has not been implemented for this operating system.");
}
private static long getFreeSpaceOnWindows(String path, String charset) throws Exception {
long bytesFree = -1;
File script = new File(System.getProperty("java.io.tmpdir"), "script.bat");
PrintWriter writer = new PrintWriter(new FileWriter(script, false));
writer.println("dir /"" + path + "/"");
writer.close();
// get the output from running the .bat file
Process p = Runtime.getRuntime().exec(script.getAbsolutePath());
InputStream reader = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;) {
int c = reader.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();
reader.close();
outputText = new String(outputText.getBytes("ISO-8859-1"), charset);
// parse the output text for the bytes free info
StringTokenizer tokenizer = new StringTokenizer(outputText, "/n");
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
// see if line contains the bytes free information
if (line.endsWith("bytes free")) {
tokenizer = new StringTokenizer(line, " ");
tokenizer.nextToken();
tokenizer.nextToken();
bytesFree = Long.parseLong(tokenizer.nextToken().replaceAll(",", ""));
} else if (line.endsWith("可用字节")) {
String[] parts = line.split(" ");
bytesFree = Long.parseLong(parts[2].replaceAll(",", ""));
}
}
return bytesFree;
}
private static long getFreeSpaceOnLinux(String path) throws Exception {
long bytesFree = -1;
Process p = Runtime.getRuntime().exec("df " + "/" + path);
InputStream reader = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;) {
int c = reader.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();
reader.close();
// parse the output text for the bytes free info
StringTokenizer tokenizer = new StringTokenizer(outputText, "/n");
tokenizer.nextToken();
if (tokenizer.hasMoreTokens()) {
String line2 = tokenizer.nextToken();
StringTokenizer tokenizer2 = new StringTokenizer(line2, " ");
if (tokenizer2.countTokens() >= 4) {
tokenizer2.nextToken();
tokenizer2.nextToken();
tokenizer2.nextToken();
bytesFree = Long.parseLong(tokenizer2.nextToken());
return bytesFree;
}
return bytesFree;
}
throw new Exception("Can not read the free space of " + path + " path");
}
public static void main(String args[]) {
try {
System.out.println("Free space of /: " + getFreeSpace(""));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
// 调用代码
public static void main(String[] args) throws Exception {
System.out.println(DiskUtil.getFreeSpace("d://"));
}
- java 得到磁盘的可用空间
- java 得到磁盘的可用空间
- COS如何得到磁盘可用空间
- 【磁盘处理相关方法】2,得到磁盘总空间、可用剩余空间
- 得到磁盘上剩余空间的值
- 得到磁盘剩余空间
- window磁盘管理下面的“未分配”与“可用空间”
- 磁盘和sd卡的可用空间大小获取
- 得到系统磁盘大小和可用磁盘大小
- linux下获取磁盘的总空间大小和可用空间大小
- linux下获取磁盘的总空间大小和可用空间大小
- mac下获取磁盘的总空间大小和可用空间大小
- windows下 c++ 检测磁盘可用空间
- VC 得到磁盘的空间(总的、已用的、剩余的)
- 得到SQLSERVER 2005 数据库的当前数据库中的数据文件所在磁盘的剩余空间
- Sql Server查询磁盘的可用空间,数据库数据文件及日志文件的大小及利用率
- Sql Server查询磁盘的可用空间,数据库数据文件及日志文件的大小及利用率
- 怎样查询磁盘的可用空间,数据库数据文件及日志文件大小及利用率
- EasyJF团队筹备小组第一次会议情况通告
- 在EasyDBO中使用Java注解配置映射的原理及使用
- Windows API一日一练(71)GetComputerName函数
- 走出Spring“轮子”的误区
- EasyDBO-0.6.0中事务处理中的严重Bug
- java 得到磁盘的可用空间
- EasyJF招聘专职工作员
- “Spring轮子血案”续集1-“回应”与“反回应”
- “中文问题没商量”之Spring2.0项目中的Bug一例
- 一个“Spring轮子”引发的“血案”(2)
- Velocity脚本基本语法简介
- Velocity脚本基本语法简介
- EasyJF招聘专职工作员
- 在Spring2中使用引介