java file获取目录硬盘己用空间与可用空间代码
获取指定目录空间大小
import java.io.File;
public class SpaceChecker {
public static void main(String[] args) {
File[] roots = File.listRoots();for (int i = 0; i < roots.length; i++) {
System.out.println(roots[i]);
System.out.println("Free space = " + roots[i].getFreeSpace());
System.out.println("Usable space = " + roots[i].getUsableSpace());
System.out.println("Total space = " + roots[i].getTotalSpace());
System.out.println();
}
}
}
获取目录空间import java.io.File;
public class SpaceChecker {
public static void main(String[] args) {
File[] roots = File.listRoots();for (int i = 0; i < roots.length; i++) {
System.out.println(roots[i]);
System.out.println("Free space = " + roots[i].getFreeSpace());
System.out.println();
}
}
}获取使用空间
import java.io.File;
public class SpaceChecker {
public static void main(String[] args) {
File[] roots = File.listRoots();for (int i = 0; i < roots.length; i++) {
System.out.println(roots[i]);
System.out.println("Usable space = " + roots[i].getUsableSpace());
System.out.println();
}
}
}获取总空间
import java.io.File;
public class SpaceChecker {
public static void main(String[] args) {
File[] roots = File.listRoots();for (int i = 0; i < roots.length; i++) {
System.out.println(roots[i]);
System.out.println("Total space = " + roots[i].getTotalSpace());
System.out.println();
}
}
}