import java.io.*;
import java.text.*;
public class DiskRoot
{
public static void main(String args[]) throws IOException
{
System.out.println("驱动器\t磁盘容量\t已用空间\t可用空间");
//获取所有的根目录对象
File []fRootList=File.listRoots(); //Fill类的listRoot()静态方法获取到各根目录对象
for(int i=0;i<fRootList.length;i++)
{
String sDrive=fRootList[i].getAbsolutePath();
long bTotal=fRootList[i].getTotalSpace();
long bFree=fRootList[i].getFreeSpace();
long bUsed=bTotal-bFree;
System.out.println(sDrive+"\t"+returnGB(bTotal)+"\t\t"+returnMB(bUsed)+"\t\t"+returnMB(bFree));
}
}
//对字节为单位的数字,转换为GB为单位(保留两位小数)
private static String returnGB(double dArg)
{
//DecimalFormat类对象可以指定对数字的格式化方式。#占位符表示此处有数字则显示,无数字就不用显示。而用0占位符的位置,无数字时要用0来填充。
DecimalFormat df=new DecimalFormat("####0.00GB");
return df.format(dArg/(1024.0*1024.0*1024.0));
}
//对字节为单位的数字,转换为MB为单位
private static String returnMB(double dArg)
{
DecimalFormat df=new DecimalFormat("######0MB");
return df.format(dArg/(1024.0*1024.0));
}
}