JAVA獲取磁盤空間


需要把磁盤空間顯示在頁面上,這樣就不用去服務器查看了,方便

兩個辦法

 File file = new File("D:");
        long totalSpace = file.getTotalSpace();
        long freeSpace = file.getFreeSpace();
        long usedSpace = totalSpace - freeSpace;
        System.out.println("總空間大小 : " + totalSpace / 1024 / 1024 / 1024 + "G");
        System.out.println("剩余空間大小 : " + freeSpace / 1024 / 1024 / 1024 + "G");
        System.out.println("已用空間大小 : " + usedSpace / 1024 / 1024 / 1024 + "G");
    }
}

方法2:

 File diskPartition = new File("D:");
        long totalCapacity = diskPartition.getTotalSpace();
        long freePartitionSpace = diskPartition.getFreeSpace();
        long usablePatitionSpace = diskPartition.getUsableSpace();
        System.out.println("**** 以M為單位****\n");
        System.out.println("總空間大小 : " + totalCapacity / (1024 * 1024) + " MB");
        System.out.println("已用空間大小 : " + usablePatitionSpace / (1024 * 1024) + " MB");
        System.out.println("剩余空間大小 : " + freePartitionSpace / (1024 * 1024) + " MB");
        System.out.println("\n**** 以G為單位 ****\n");
        System.out.println("總空間大小 : " + totalCapacity / (1024 * 1024 * 1024) + " GB");
        System.out.println("已用空間大小 : " + usablePatitionSpace / (1024 * 1024 * 1024) + " GB");
        System.out.println("剩余空間大小 : " + freePartitionSpace / (1024 * 1024 * 1024) + " GB");

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM