本文分享了兩段獲取磁盤空間的代碼,參考下。
代碼1:
import java.io.File;public class DiskSpaceDetail {public static void main(String[] args) {File diskPartition = new File("C:");long totalCapacity = diskPartition.getTotalSpace();long freePartitionSpace = diskPartition.getFreeSpace();long usablePatitionSpace = diskPartition.getUsableSpace();System.out.println("**** Sizes in Mega Bytes ****/n");System.out.println("Total C partition size : " + totalCapacity / (1024*1024) + " MB");System.out.println("Usable Space : " + usablePatitionSpace / (1024 *1024) + " MB");System.out.println("Free Space : " + freePartitionSpace / (1024 *1024) + " MB");System.out.println("/n**** Sizes in Giga Bytes ****/n");System.out.println("Total C partition size : " + totalCapacity / (1024*1024*1024) + " GB");System.out.println("Usable Space : " + usablePatitionSpace / (1024 *1024*1024) + " GB");System.out.println("Free Space : " + freePartitionSpace / (1024 *1024*1024) + " GB");}}運行結果
代碼2:
public class FreeDiskSpace {public static void main(String[] args) {File file = new File("c:");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");}}結果:
總結
哈哈,讓大家見笑了。
以上就是本文關於Java獲取磁盤空間的兩種代碼示例的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!