首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java代码从msinfo32文件中提取特定行

如何使用java代码从msinfo32文件中提取特定行
EN

Stack Overflow用户
提问于 2019-10-04 18:19:41
回答 1查看 163关注 0票数 0

我正在写一个代码来提取系统信息的细节,即内存,处理器速度,并将它们放在一个文本文件中。

代码语言:javascript
复制
public void getSpecs(){
    //run a cmd command to convert msinfo32 to .txt file
   String[] command = {
        "cmd",
    };
    Process p;
    try{
        p= Runtime.getRuntime().exec(command);
        new Thread(new Sec(p.getErrorStream(), System.err)).start();
        new Thread(new Sec(p.getInputStream(), System.out)).start();
        PrintWriter pw= new PrintWriter(p.getOutputStream());

        pw.println("msinfo32 /report .\\specs.txt");
        pw.close();
        p.waitFor();
    }catch(Exception e){
        e.printStackTrace();
    }   
  }
}

这个过程花费了很长的时间,并且转换了整个文件。

EN

回答 1

Stack Overflow用户

发布于 2019-10-04 19:19:29

msinfo32将计算机信息导出到文件中。预计这将花费一些时间,因为它检索每个计算机/windows组件的大量导出。

我已经使用powershell做了类似的事情

代码语言:javascript
复制
    public static void main(String[] args) throws IOException {
        //Set the commands
        String cmd = "powershell.exe  get-WmiObject ";
        String[] win32CmdA = {"win32_processor", "win32_computerSystem", "win32_logicaldisk"};

        for (String win32Cmd : win32CmdA) {
            String info = runCmd(cmd + win32Cmd,
                    "MaxClockSpeed",
                    "TotalPhysicalMemory",
                    "DeviceID",
                    "FreeSpace");//Add as many atributes you want to return from powershell output
            System.out.println(info); // You can use a file writer here
        }

//        //You can handle ErrorStream here
//        String line;
//        BufferedReader stderr = new BufferedReader(new InputStreamReader(
//                powerShellProcess.getErrorStream()));
//        while ((line = stderr.readLine()) != null) {
//            System.out.println(line);
//        }
    }

    private static String runCmd(String cmd, String... attrs) throws IOException {
        Process powerShellProcess = Runtime.getRuntime().exec(cmd);
        powerShellProcess.getOutputStream().close();

        String line;
        String result="";
        BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
        while ((line = stdout.readLine()) != null) {
            if (line != null && line.contains(":")) {
                String nameValue[] = line.split(":");
                if (Arrays.asList(attrs).contains(nameValue[0].trim())) {
                    result+=nameValue[0] + " - " + nameValue[1] + "\n";
                }
            }
        }
        stdout.close();
        return result;
    }

上面的代码为特定组件(处理器、computerSystem和逻辑磁盘)调用powershell中的Windows Management Instrumentation (WMI)类。

然后定义应该从powershell输出中获取哪些值,如MaxClockSpeed、TotalPhysicalMemory等。

如果您使用文件编写器更改System.out.println( info );,则此信息将保存在文件中。

示例输出(运行耗时约3秒)

代码语言:javascript
复制
DeviceID           -  CPU0
MaxClockSpeed      -  3401

TotalPhysicalMemory  -  17053949952

DeviceID      -  C
FreeSpace     -  56341774336
DeviceID      -  D
FreeSpace     -  
DeviceID      -  F
FreeSpace     -  373687742464
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58234465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档