1. 程式人生 > >java獲取磁碟使用資訊

java獲取磁碟使用資訊

獲取磁碟使用資訊

package com.igustudio.tmanager.utils;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 磁碟工具類
 * 
 * @author gu
 * @date  2017年12月22日
 */
public class DiskFreeUtils {
	
	private static DecimalFormat DECIMALFORMAT = new DecimalFormat("#.##");

	/**
	 * 獲取磁碟使用資訊
	 * 
	 * @return
	 */
	public static List<Map<String, String>> getInfo() {

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		File[] roots = File.listRoots();// 獲取磁碟分割槽列表
		for (File file : roots) {
			Map<String, String> map = new HashMap<String, String>();
			
			long freeSpace=file.getFreeSpace();
			long totalSpace=file.getTotalSpace();
			long usableSpace=totalSpace-freeSpace;
			
			map.put("path", file.getPath());
			map.put("freeSpace", freeSpace / 1024 / 1024 / 1024 + "G");// 空閒空間
			map.put("usableSpace", usableSpace / 1024 / 1024 / 1024 + "G");// 可用空間
			map.put("totalSpace",totalSpace / 1024 / 1024 / 1024 + "G");// 總空間
			map.put("percent", DECIMALFORMAT.format(((double)usableSpace/(double)totalSpace)*100)+"%");// 總空間
			
			list.add(map);
		}

		return list;
	}

	public static void main(String[] args) {
		
		System.out.println(getInfo());
		
	}

}

獲取到的資訊如下: