成都创新互联网站制作重庆分公司

Java获取磁盘空间的两种代码示例

本文分享了两段获取磁盘空间的代码,参考下。

成都创新互联公司是一家业务范围包括IDC托管业务,雅安服务器托管、主机租用、主机托管,四川、重庆、广东电信服务器租用,雅安电信机房,成都网通服务器托管,成都服务器租用,业务范围遍及中国大陆、港澳台以及欧美等多个国家及地区的互联网数据服务公司。

代码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");
	}
}

运行结果

Java获取磁盘空间的两种代码示例

代码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获取磁盘空间的两种代码示例

总结

哈哈,让大家见笑了。

以上就是本文关于Java获取磁盘空间的两种代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!


分享文章:Java获取磁盘空间的两种代码示例
分享路径:http://cxhlcq.com/article/gjcgcd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部