在java中怎样获取本地硬盘容量
1个回答
展开全部
import java.io.File;
import java.text.DecimalFormat;
/**
* User:
* Date: 2016/12/07
* Time: 14:29
*/
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;
/**
* User:
* Date: 2016/12/07
* Time: 14:29
*/
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);
}
}
试一下。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询