一般来讲,要用java得到硬盘空间,有3种方法: 1. 调用system的command,然后分析得到的结果,这种方法有很强的系统依赖性,linux下和win下要分别写程序 下面是一个win下的例子,编译成功之后,运行java Diskspace yourdir(比如c:\) import java.io.BufferedReader; import java.io.InputStreamReader;/** * 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, it's that shaky. * Requires Java 1.4 or higher. * @[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 -1 if unknown */ public static long getFreeDiskSpace(String dirName) { try { // guess correct 'dir' command by looking at the // operating system name String os = System.getProperty("os.name"); String command; if (os.equals("Windows NT") || os.equals("Windows 2000")) { command = "cmd.exe /c dir " + dirName; } else { command = "command.com /c dir " + dirName; } // run the dir command on the argument directory name Runtime runtime = Runtime.getRuntime(); Process process = null; process = runtime.exec(command); if (process == null) { return -1; } // read the output of the dir command // only the last line is of interest BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream())); String line; String freeSpace = null; while ((line = in.readLine()) != null) { 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之即可.
如何列出windows下的逻辑分区?
public void ListDisks() { File[] roots = File.listRoots(); for (int i=0; i
System.out.println(roots[i]); } } 如何获得可用的硬盘空间?
目前尚未发现有任何干净利落的纯Java方法能够解决这个问题。通常的解决方案是直接访问操作系统获得这些信息。有一个被称为JConfig的类库提供了一些方法可以获得磁盘和文件信息,但是可以肯定这个类库使用了JNI方法。
下载地址:http://www.tolstoy.com/samizdat/jconfig.html 如果你使用的是晕倒死系列操作系统,那么下面的方法也许能够获得正确的结果。我之所以说也许,是因为我在多个晕倒死平台上作过实际测试,在英文版的晕倒死上基本上都能够得到正确的结果,在中文版的晕倒死上基本上都不能够获得正确的结果。 String osname = System.getProperty("os.name"); String command = ""; if (osname.indexOf("NT") > -1) command = "c:\\winnt\\System32\\cmd.exe"; else if (osname.indexOf("Windows") > -1) command = "c:\\windows\\command.com"; Process p = Runtime.getRuntime().exec( command + " /c dir > c:\\dir.txt"); p.waitFor();
希望对你有点帮助
而我本来也想用java做的
后来还是用c++BUILDER完成了
wyi202 (英子) 如果找到好的请发给我
我也在学java(jbuilder7) [email protected]
command + " /c dir > c:\\dir.txt"其中/c 怎么讲?多谢。在我的中文晕倒死上测试可以
好像是调用DOS下的dir命令,察看c:\\dir.txt文件
我也不太清楚,别人给的,我能调通就不错了,搞清了记得啊告诉我啊
我其实很菜,我才学JAVA3周,刚入门而已。我们多交流啊
64304318
把调好的程序发给我好吗
[email protected]
1. 调用system的command,然后分析得到的结果,这种方法有很强的系统依赖性,linux下和win下要分别写程序
下面是一个win下的例子,编译成功之后,运行java Diskspace yourdir(比如c:\)
import java.io.BufferedReader;
import java.io.InputStreamReader;/**
* 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, it's that shaky.
* Requires Java 1.4 or higher.
* @[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 -1 if unknown
*/
public static long getFreeDiskSpace(String dirName)
{
try
{
// guess correct 'dir' command by looking at the
// operating system name
String os = System.getProperty("os.name");
String command;
if (os.equals("Windows NT") ||
os.equals("Windows 2000"))
{
command = "cmd.exe /c dir " + dirName;
}
else
{
command = "command.com /c dir " + dirName;
}
// run the dir command on the argument directory name
Runtime runtime = Runtime.getRuntime();
Process process = null;
process = runtime.exec(command);
if (process == null)
{
return -1;
}
// read the output of the dir command
// only the last line is of interest
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
String freeSpace = null;
while ((line = in.readLine()) != null)
{
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之即可.