获取磁盘总空间和可用空间问题

sxlcom1 2014-08-15 09:54:16
以下代码在win 7下运行正常,
但在windows 2003却运行在 while ((line = br.readLine()) != null)
出现堵塞情况,请问大家是否有处理方法可以借鉴个一下,先谢了!

public class GoodWindowsExec {
public static void main(String args[]) {

try {

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("wmic LogicalDisk where Caption='d:' get FreeSpace,Size");
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");

StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");

errorGobbler.start();
outputGobbler.start();
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);

} catch (Throwable t) {
t.printStackTrace();
}
}
}
--------------------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class StreamGobbler extends Thread {
InputStream is;
String type;
OutputStream os;
StreamGobbler(InputStream is, String type) {
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect) {
this.is = is;
this.type = type;
this.os = redirect;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type.equals("Error"))
System.out.println(line);
else
System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
...全文
122 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxlcom1 2014-08-16
  • 打赏
  • 举报
回复
我在windows 2003 的cmd运行wmic LogicalDisk where Caption='d:' get FreeSpace,Size 也是可以返回D盘的总容量和可有效使用容量 但通过程序运行总在while ((line = br.readLine()) != null) 获取信息堵塞,有解决的方法吗?先谢了
sxlcom1 2014-08-16
  • 打赏
  • 举报
回复
请版主出现帮一下忙

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧