java 能不能获取CPU的ID号,硬盘的序列号

2024-12-26 13:54:23
推荐回答(2个)
回答1:

public class CpuUtil {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
Process process = Runtime.getRuntime().exec(
new String[] { "wmic", "cpu", "get", "ProcessorId" });
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
String property = sc.next();
String serial = sc.next();
System.out.println(property + ": " + serial);
System.out.println("time:" + (System.currentTimeMillis() - start));
}
}
public class DiskUtil {
public static String getSerialNumber(String drive) {
String result = "";
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
+"Set colDrives = objFSO.Drives\n"
+"Set objDrive = colDrives.item(\"" + drive + "\")\n"
+"Wscript.Echo objDrive.SerialNumber"; // see note
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
return result.trim();
}
public static void main(String[] args) {
String sn = DiskUtil.getSerialNumber("C");
System.out.println(sn);
}

回答2:

获取CPU序列号

Process process = Runtime.getRuntime().exec(  
    new String[] { "wmic", "cpu", "get", "ProcessorId" });  
process.getOutputStream().close();  
Scanner sc = new Scanner(process.getInputStream());  
String property = sc.next();  
String serial = sc.next();  
System.out.println(property + ": " + serial);