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));
}
}