使用 Java 获取CPU序列号

本文介绍在 Windows 下怎样利用 Java 获取 CPU 序列号。

下面是在 Window10 系统平台通过 Java 代码获取 CPU 序列号的示例:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {
        Demo demo = new Demo();
        String[] serials = demo.getCPUSerialNo();
        for(String serial : serials) {
            System.out.println(serial);
        }
    }

    /**
     * 获取CPU序列号
     * wmic cpu get ProcessorId
     * @return
     */
    private String[] getCPUSerialNo() {
        List<String> codeList = new ArrayList<>();
        Scanner sc = null;
        try {
            Process process = Runtime.getRuntime().exec(
                    new String[]{"wmic", "cpu", "get", "ProcessorId"});
            process.getOutputStream().close();
            sc = new Scanner(process.getInputStream());
            sc.next(); // ProcessorId
            while(sc.hasNext()) {
                codeList.add(sc.next());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(null != sc) {
                sc.close();
            }
        }
        return codeList.toArray(new String[]{});
    }

}

运行示例,输出如下:

BFEBF******40651
一个不注意小事情的人,永远不会成功大事业。——戴尔·卡耐基
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号