import java.io.File;public class SpaceChecker {
public static void main(String[] args) { //File win = new File("C:/");
File win = new File("D:/");
System.out.println(win.getPath());
System.out.println(win.getName());
System.out.println("Free space = " + win.getFreeSpace());
System.out.println("Usable space = " + win.getUsableSpace());
System.out.println("Total space = " + win.getTotalSpace());
Runtime r = Runtime.getRuntime();
float freeMemory = (float) r.freeMemory();
float totalMemory = (float) r.totalMemory();
System.out.println("freeMemory"+ freeMemory);
System.out.println("totalMemory"+totalMemory);
}
}
可是我要获取整个电脑硬盘的大小信息咋处理啊?
public static void main(String[] args) { //File win = new File("C:/");
File win = new File("D:/");
System.out.println(win.getPath());
System.out.println(win.getName());
System.out.println("Free space = " + win.getFreeSpace());
System.out.println("Usable space = " + win.getUsableSpace());
System.out.println("Total space = " + win.getTotalSpace());
Runtime r = Runtime.getRuntime();
float freeMemory = (float) r.freeMemory();
float totalMemory = (float) r.totalMemory();
System.out.println("freeMemory"+ freeMemory);
System.out.println("totalMemory"+totalMemory);
}
}
可是我要获取整个电脑硬盘的大小信息咋处理啊?
windows一般读注册表,
win.getFreeSpace(); win.getUsableSpace();win.getTotalSpace() 方法不存在你用的那个jdk 版本啊
for (File _file : roots) {
}
可以把各个盘符的大小取出来。但是最要命的是把链接服务器的那个硬盘也取出来了。
import java.io.File;
import java.text.DecimalFormat;public class Test {
public static void main(String[] args) {
File[] roots = File.listRoots();
double constm = 1024 * 1024 * 1024 ;
double total = 0d;
for (File _file : roots) {
System.out.println(_file.getPath());
System.out.println("剩余空间 = " + doubleFormat(_file.getFreeSpace()/constm)+" G");
System.out.println("已使用空间 = " + doubleFormat(_file.getUsableSpace()/constm)+" G");
System.out.println(_file.getPath()+"盘总大小 = " + doubleFormat(_file.getTotalSpace()/constm)+" G");
System.out.println();
total+=_file.getTotalSpace();
}
System.out.println("你的硬盘总大小 = "+doubleFormat(total/constm));
}
public static String doubleFormat(double d){
DecimalFormat df = new DecimalFormat("0.##");
return df.format(d);
}
}
usable space: 是可用空间的意思,不是已用空间