{
freeSpace = line;
}
if (freeSpace == null)
{
return -1;
}
process.destroy();
// remove dots & commas & leading and trailing whitespace
freeSpace = freeSpace.trim();
freeSpace = freeSpace.replaceAll("\\.", "");
freeSpace = freeSpace.replaceAll(",", "");
String[] items = freeSpace.split(" ");
// the first valid numeric value in items after(!) index 0
// is probably the free disk space
int index = 1;
while (index < items.length)
{
try
{
long bytes = Long.parseLong(items[index++]);
return bytes;
}
catch (NumberFormatException nfe)
{
}
}
return -1;
}
catch (Exception exception)
{
return -1;
}
}
/**
* Command line program to print the free diskspace to stdout
* for all 26 potential root directories A:\ to Z:\
* (when no parameters are given to this program)
* or for those directories (drives) specified as parameters.
* @[EMAIL PROTECTED]
args program parameters
*/
public static void main(String[] args)
{
if (args.length == 0)
{
for (char c = 'A'; c <= 'Z'; c++)
{
String dirName = c + ":\\";
System.out.println(dirName + " " +
getFreeDiskSpace(dirName));
}
}
else
{
for (int i = 0; i < args.length; i++)
{
System.out.println(args[i] + " " + getFreeDiskSpace(args[i]));
}
}
}
}
方法二:使用Jconfig,可以跨平台
從http://www.tolstoy.com/samizdat/jconfig.html上下載jconfig。
下載的包的sample裏有很簡單的例子,如果是要得到磁盤空間的話:
用FileRegistry.getVolumes()得到DiskVolume。
然後call getFreeSpace()和getMaxCapacity()。
就是這麼簡單。
方法三:jni
這個是解決所有和os相關的操作的萬能利器了。
例子我也懶得寫了。
寫一個dll然後call之即可。