使用sigar获取计算机cpu型号,数量 内存总量 磁盘总量

烈酒焚心 提交于 2019-11-30 09:26:37
public static ComputerModel getComputerMessage() {
    try {
        ComputerModel computerModel = new ComputerModel();
        long totalDisk=0;
        Sigar sigar = new Sigar();
        CpuInfo infos[] = sigar.getCpuInfoList();
        Mem mem = sigar.getMem();
        String totalMem = bytes2GB(mem.getTotal());
        CpuPerc cpuList[] = null;
        cpuList = sigar.getCpuPercList();
        CpuInfo info = infos[0];
        FileSystem fslist[] = sigar.getFileSystemList();
        for (int i = 0; i < fslist.length; i++) {
            FileSystem fs = fslist[i];
            FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
            switch (fs.getType()) {
                case 2: // TYPE_LOCAL_DISK : 本地硬盘
                    // 文件系统总大小
                    totalDisk += usage.getTotal();
                    break;
            }
        }

        computerModel.setCpuModel(info.getModel());
        computerModel.setTotleMemory(totalMem);
        computerModel.setTotleDisk((totalDisk/1024/1024)+"");
        computerModel.setCpuCount(infos.length);
        return computerModel;
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}
public static String bytes2GB(long bytes) {
    DecimalFormat df = new DecimalFormat("#0.00");
    return df.format((double) bytes / 1024/1024/1024);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!