Java獲取磁盤目錄大小

用File API可以獲取指定目錄的大小

freeSpace和usableSpace區(qū)別

FreeSpace:
Returns the number of unallocated bytes in the partition named by this abstract path name.
The returned number of unallocated bytes is a hint, but not a guarantee, that it is possible to use most or any of these bytes. The number of unallocated bytes is most likely to be accurate immediately after this call. It is likely to be made inaccurate by any external I/O operations including those made on the system outside of this virtual machine. This method makes no guarantee that write operations to this file system will succeed.

UsableSpace:
Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname. When possible, this method checks for write permissions and other operating system restrictions and will therefore usually provide a more accurate estimate of how much new data can actually be written than getFreeSpace.
The returned number of available bytes is a hint, but not a guarantee, that it is possible to use most or any of these bytes. The number of unallocated bytes is most likely to be accurate immediately after this call. It is likely to be made inaccurate by any external I/O operations including those made on the system outside of this virtual machine. This method makes no guarantee that write operations to this file system will succeed.

代碼

    public static void main(String[] args) {
        File root = new File("/");
        System.out.println(root.getTotalSpace()); //500068036608
        System.out.println(root.getFreeSpace()); //44024229888
        System.out.println(root.getUsableSpace()); //30446862336

        System.out.println(readableFileSize(root.getTotalSpace())); //465.7 GB
        System.out.println(readableFileSize(root.getFreeSpace())); //41 GB
        System.out.println(readableFileSize(root.getUsableSpace())); //31.3 GB
    }

    /**
     * 格式化文件大小
     * 參考:https://stackoverflow.com/a/5599842/1253611
     *
     * @param size byte
     * @return readable file size
     */
    public static String readableFileSize(long size) {
        if (size <= 0) return "0";
        final String[] units = new String[]{"B", "kB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,866評(píng)論 0 10
  • 今天注定是一個(gè)個(gè)錯(cuò)誤的預(yù)算結(jié)成的障礙,為了提前取票在路上花了一個(gè)下午,東西沒(méi)有整理完,核對(duì)工資又排隊(duì)1個(gè)半小時(shí),未完無(wú)續(xù)
    宋小朝閱讀 183評(píng)論 0 0
  • 剛被林心如大婚虐完一個(gè)月,這下又被舒淇虐了,這兩位黃金剩女終于都婚了!而且,都嫁給了相識(shí)多年的老朋友! 這是什么套...
    可聞桃杏香閱讀 585評(píng)論 0 2
  • 一、理解prototype 我們創(chuàng)建的每一個(gè)函數(shù)都有一個(gè)prototype屬性,這個(gè)屬性是一個(gè)指向?qū)ο蟮闹羔槨?構(gòu)...
    三杯兩盞淡茶閱讀 3,295評(píng)論 0 5
  • 夜幕降臨,內(nèi)心不知是恐懼害怕,還是擔(dān)心亦或是期待,對(duì)于這樣的你來(lái)說(shuō),你覺(jué)得是你想要的嗎?或許有些事情再怎么強(qiáng)求,也...
    靖蘭兒閱讀 163評(píng)論 0 0

友情鏈接更多精彩內(nèi)容