Java获取磁盘空间的两种代码示例
本文分享了两段获取磁盘空间的代码,参考下。
代码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获取磁盘空间的两种代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
赞 (0)