在java中怎样获取本地硬盘容量
答案:1 悬赏:20
解决时间 2021-02-08 03:13
- 提问者网友:暖心后
- 2021-02-07 13:52
在java中怎样获取本地硬盘容量
最佳答案
- 二级知识专家网友:闲懒诗人
- 2021-02-07 14:57
import java.io.File;
import java.text.DecimalFormat;
public class MainTest {
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));
}
private static String doubleFormat(double d){
DecimalFormat df = new DecimalFormat("0.##");
return df.format(d);
}
}
试一下。。。
import java.text.DecimalFormat;
public class MainTest {
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));
}
private static String doubleFormat(double d){
DecimalFormat df = new DecimalFormat("0.##");
return df.format(d);
}
}
试一下。。。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯