顯示Windows作業系統下的各盤符的磁碟空間資訊

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.