1. 程式人生 > >JAVA獲取磁碟空間

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