判断window,还是linux系统,查询磁盘空间

1.判断是否为window系统

private static boolean isWindow() {
    return System.getProperty("os.name").toLowerCase().contains("win");
}

2.判断是否为linux系统

private static boolean isLinux() {
    return System.getProperty("os.name").toLowerCase().contains("linux");
}

3.window 查询硬盘空间

查询所有分区的硬盘空间

private static void getDiskInfoByPath() {
    File[] disks = File.listRoots();
    for (File file : disks) {
        System.out.print(file.getPath() + "    ");
        // 空闲空间
        System.out.print("空闲未使用 = " + file.getFreeSpace() / 1024 / 1024 + "M" + "    ");
        // 已用空间
        System.out.print("已经使用 = " + file.getUsableSpace() / 1024 / 1024 + "M" + "    ");
        // 总空间
        System.out.print("总容量 = " + file.getTotalSpace() / 1024 / 1024 + "M" + "    ");
        System.out.println();
    }
}

查询指定分区的硬盘空间

4.linux 查询硬盘空间

linux 查询磁盘空间命令为df -h

查询某一个文件系统的容量

df -h 名称

 

 

5全部代码

package com.ibeetl.admin.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * 硬盘工具类
 *
 * @author chenye
 * @date 20190124
 */
public class DiskUtil {

    public static void main(String[] args) {
        System.out.println(getDiskInfo("D:\\"));
        System.out.println(isWindow());
        getDiskInfoByPath();
    }

    public static Map getDiskInfo(String path) {
        //判断是否为linux 系统
        if (isLinux()) {
            //是linux系统
            return getLinuxDiskInfo(path);
        } else if (isWindow()) {
            //是window
            return getWindowDiskInfo(path);
        }
        return null;
    }

    private static void getDiskInfoByPath() {
        File[] disks = File.listRoots();
        for (File file : disks) {
            System.out.print(file.getPath() + "    ");
            // 空闲空间
            System.out.print("空闲未使用 = " + file.getFreeSpace() / 1024 / 1024 + "M" + "    ");
            // 已用空间
            System.out.print("已经使用 = " + file.getUsableSpace() / 1024 / 1024 + "M" + "    ");
            // 总空间
            System.out.print("总容量 = " + file.getTotalSpace() / 1024 / 1024 + "M" + "    ");
            System.out.println();
        }
    }

    @SuppressWarnings("unchecked")
    private static Map getWindowDiskInfo(String path) {
        File file = new File(path);
        Map map = new HashMap(3);
        // 空闲空间
        map.put("free", file.getFreeSpace() / 1024 / 1024);
        // 已用空间
        map.put("used", (file.getTotalSpace() - file.getFreeSpace()) / 1024 / 1024);
        // 总空间
        map.put("total", file.getTotalSpace() / 1024 / 1024);
        return map;
    }
    @SuppressWarnings("unchecked")
    private static Map getLinuxDiskInfo(String path) {
        Map map = new HashMap(5);
        try {
            Runtime rt = Runtime.getRuntime();
            // df -hl 查看硬盘空间
            Process p = rt.exec("df -hl" + path);
            BufferedReader in = null;
            try {
                in = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String str = null;
                String[] strArray = null;
                int line = 0;
                while ((str = in.readLine()) != null) {
                    line++;
                    if (line != 2) {
                        continue;
                    }
                    int m = 0;
                    strArray = str.split(" ");
                    for (String para : strArray) {
                        if (para.trim().length() == 0) {
                            continue;
                        }
                        ++m;
                        if (para.endsWith("G") || para.endsWith("Gi")) {
                            // 目前的服务器
                            if (m == 2) {
                                map.put("total", para);
                            }
                            if (m == 3) {
                                map.put("used", para);
                            }
                        }
                        if (para.endsWith("%")) {
                            if (m == 5) {
                                map.put("use_rate", para);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 判断是否为linux系统
     */
    private static boolean isLinux() {
        return System.getProperty("os.name").toLowerCase().contains("linux");
    }

    /**
     * 判断是否为window系统
     */
    private static boolean isWindow() {
        return System.getProperty("os.name").toLowerCase().contains("win");
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java知路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值