一般來講
下面是一個win下的例子
import java
import java
/**
* Determine free disk space for a given directory by
* parsing the output of the dir command
* This class is inspired by the code at
* Works only under Windows under certain circumstances
* Yes
* Requires Java
* @[EMAIL PROTECTED]
*Marco Schmidt
*/
public class Diskspace
{
private Diskspace()
{
// prevent instantiation of this class
}
/**
* Return available free disk space for a directory
* @[EMAIL PROTECTED]
dirName name of the directory
* @[EMAIL PROTECTED]
free disk space in bytes or
*/
public static long getFreeDiskSpace(String dirName)
{
try
{
// guess correct
// operating system name
String os = System
String command;
if (os
os
{
command =
}
else
{
command =
}
// run the dir command on the argument directory name
Runtime runtime = Runtime
Process process = null;
process = runtime
if (process == null)
{
return
}
// read the output of the dir command
// only the last line is of interest
BufferedReader in = new BufferedReader(
new InputStreamReader(process
String line;
String freeSpace = null;
while ((line = in
{
freeSpace = line;
}
if (freeSpace == null)
{
return
}
process
// remove dots & commas & leading and trailing whitespace
freeSpace = freeSpace
freeSpace = freeSpace
freeSpace = freeSpace
String[] items = freeSpace
// the first valid numeric value in items after(!) index
// is probably the free disk space
int index =
while (index < items
{
try
{
long bytes = Long
return bytes;
}
catch (NumberFormatException nfe)
{
}
}
return
}
catch (Exception exception)
{
return
}
}
/**
* Command line program to print the free diskspace to stdout
* for all
* or for those directories (drives) specified as parameters
* @[EMAIL PROTECTED]
args program parameters
*/
public static void main(String[] args)
{
if (args
{
for (char c =
{
String dirName = c +
System
getFreeDiskSpace(dirName));
}
}
else
{
for (int i =
{
System
getFreeDiskSpace(args[i]));
}
}
}
}
方法二:使用Jconfig
從上下載jconfig
下載的包的sample裡有很簡單的例子
用FileRegistry
然後call getFreeSpace()和getMaxCapacity()
就是這麼簡單
方法三:jni
這個是解決所有和OS相關操作的萬能利器了
例子我也懶得寫了
寫一個dll然後call之即可
From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25268.html