import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author clsun88
* @version 1.0
* @date 2008-12-10
* @class_displayName DiskSpace
*/
public class DiskSpace {
public static final int OS_Unknown = 0;
public static final int OS_WinNt = 1;
public static final int OS_Win9x = 2;
public static final int OS_Unix = 3;
public static final int OS_Linux = 4;
public static String _os = System.getProperty("os.name");
public static final String CRLF = System.getProperty("line.separator");
protected static int os_type(){
String os = _os.toUpperCase();
if (os.startsWith("WINDOWS")){
if (os.endsWith("NT") || os.endsWith("XP") || os.endsWith("2000")|| os.endsWith("2003"))
return OS_WinNt;
else return OS_Win9x;
}else if (os.indexOf("un")>0) return OS_Unix;
else if (os.indexOf("LINUX")>0)return OS_Linux;
else return OS_Unknown;
}
protected static long os_freesize() {
String[] cmds = null;
String dirName = "c://";
long freeSize = -1;
int osType = os_type();
switch(osType) {
case OS_WinNt:
cmds = new String[]{"cmd.exe", "/c", "dir", dirName};
freeSize = os_freesize_win(os_exec(cmds));
break;
case OS_Win9x:
cmds = new String[]{"command.exe", "/c", "dir", dirName};
freeSize = os_freesize_win(os_exec(cmds));
break;
case OS_Linux:
case OS_Unix:
cmds = new String[]{"df"};
freeSize = os_freesize_unix(os_exec(cmds));
break;
default:
}
return freeSize;
}
protected static String os_exec(String[] cmds) {
int ret = 0;
Process porc = null;
InputStream perr = null, pin = null;
StringBuffer sb = new StringBuffer();
String line = null;
try {
// for(int i=0; i
//porc = Runtime.getRuntime().exec(cmds);//执行编译操作
porc = Runtime.getRuntime().exec(cmds, null, null);
perr = porc.getErrorStream();
pin = porc.getInputStream();
//获取屏幕输出显示
//while((c=pin.read())!=-1) sb.append((char) c);
BufferedReader br = new BufferedReader(new InputStreamReader(pin));
while((line=br.readLine())!=null) {
// System.out.println("exec()O: "+line);
sb.append(line).append(CRLF);
}
//获取错误输出显示
br = new BufferedReader(new InputStreamReader(perr));
while((line=br.readLine())!=null) {
// System.err.println("exec()E: "+line);
}
porc.waitFor(); //等待编译完成
ret = porc.exitValue(); //检查javac错误代码
if (ret!=0) {
// _log.warn("porc.exitValue() = "+ret);
}
}catch(Exception e) {
// _log.warn("exec() "+e, e);
}finally {
porc.destroy();
}
// System.out.println(sb.toString());
return sb.toString();
}
private static long os_freesize_unix(String s) {
String lastLine = os_lastline(s); //获取最后一航;
if (lastLine == null) {
// _log.warn("(lastLine == null)");
return -1;
}else lastLine = lastLine.trim();
//格式:/dev/sda1 101086 12485 83382 14% /boot
//lastLine = lastLine.replace('/t', ' ');
String[] items = os_split(lastLine);
// _log.debug("os_freesize_unix() 目录:/t"+items[0]);
// _log.debug("os_freesize_unix() 总共:/t"+items[1]);
// _log.debug("os_freesize_unix() 已用:/t"+items[2]);
// _log.debug("os_freesize_unix() 可用:/t"+items[3]);
// _log.debug("os_freesize_unix() 可用%:/t"+items[4]);
// _log.debug("os_freesize_unix() 挂接:/t"+items[5]);
if(items[3]==null) {
// _log.warn("(ss[3]==null)");
return -1;
}
return Long.parseLong(items[3])*1024; //按字节算
}
private static long os_freesize_win(String s) {
String lastLine = os_lastline(s); //获取最后一航;
// System.out.println(lastLine);
if (lastLine == null) {
// _log.warn("(lastLine == null)");
return -1;
}else lastLine = lastLine.trim().replaceAll(",", "");
//分析
String items[] = os_split(lastLine); //15 个目录 1,649,696,768 可用字节
System.out.println(items[2]);
if (items.length<4) {
// _log.warn("DIR result error: "+lastLine);
return -1;}
if (items[2]==null) {
// _log.warn("DIR result error: "+lastLine);
return -1;}
System.out.println(items[2]);
long bytes = Long.parseLong(items[2]); //1,649,696,768
return bytes;
}
protected static String os_lastline(String s) {
//获取多行输出的最后一行;
BufferedReader br = new BufferedReader(new StringReader(s));
String line = null, lastLine=null;
try {
while((line=br.readLine())!=null) lastLine = line;
}catch(Exception e) {
// _log.warn("parseFreeSpace4Win() "+e);
}
//_log.debug("os_lastline() = "+lastLine);
// System.out.println(lastLine);
return lastLine;
}
protected static String[] os_split(String s) {
// _log.debug("os_split() "+s);
String[] ss = s.split(" "); //空格分隔;
List ssl = new ArrayList(16);
for(int i=0; i <ss.length;i++){
if (ss[i]==null) continue;
ss[i] = ss[i].trim();
if (ss[i].length()==0) continue;
ssl.add(ss[i]);
// _log.debug("os_split() "+ss[i]);
}
String[] ss2 = new String[ssl.size()];
ssl.toArray(ss2);
return ss2;
}
public static void main(String[] args) throws IOException {
System.out.println(os_freesize());
}
}