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

问题描述 投票:0回答:1

我正在编写代码以提取系统信息详细信息,即ram,处理器速度,并将其放在文本文件中。

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();
    }   
  }
}

此过程需要很长时间,并且它会转换整个文件。

java hardware system-information
1个回答
0
投票

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

我使用powershell做过类似的事情

    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;
    }

上面的代码在Powershell中为特定组件(处理器,计算机系统和逻辑磁盘)调用Windows Management Instrumentation(WMI)类。

然后您定义应从Powershell输出中获取哪些值,例如MaxClockSpeed,TotalPhysicalMemory等。>

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

样本输出(运行约3秒钟)

DeviceID           -  CPU0
MaxClockSpeed      -  3401

TotalPhysicalMemory  -  17053949952

DeviceID      -  C
FreeSpace     -  56341774336
DeviceID      -  D
FreeSpace     -  
DeviceID      -  F
FreeSpace     -  373687742464
© www.soinside.com 2019 - 2024. All rights reserved.