classfinal
classfinal copied to clipboard
机器码中的CPU、硬盘获取有问题
存在问题: 部分系统Process读到的数据会出现空行,按行数截取获取不到硬件id信息 原方法:
public static String runCmd(String cmd, int line) {
Process process;
Scanner sc = null;
StringBuffer sb = new StringBuffer();
try {
process = Runtime.getRuntime().exec(cmd);
process.getOutputStream().close();
sc = new Scanner(process.getInputStream());
int i = 0;
while (sc.hasNextLine()) {
i++;
String str = sc.nextLine();
if (line <= 0) {
sb.append(str).append("\r\n");
} else if (i == line) {
return str.trim();
}
}
sc.close();
} catch (Exception e) {
} finally {
close(sc);
}
return sb.toString();
}
本地测试结果如下: macList:[C8-B2-9B-DA-42-B0, 00-50-56-C0-00-08] c1:00aabdc6d979de09f33e0751a5bf93df cpu: c2:d41d8cd98f00b204e9800998ecf8427e harddisk: c3:d41d8cd98f00b204e9800998ecf8427e code:00AABDC6D979DE09F33E0751A5BF93DFD41D8CD98F00B204E9800998ECF8427ED41D8CD98F00B204E9800998ECF8427E
尝试修改跳过空行:
public static String runCmd(String cmd, int line) {
Process process;
Scanner sc = null;
StringBuffer sb = new StringBuffer();
try {
process = Runtime.getRuntime().exec(cmd);
process.getOutputStream().close();
sc = new Scanner(process.getInputStream());
int i = 0;
while (sc.hasNextLine()) {
String str = sc.nextLine();
if (str.isEmpty()) {
continue;
}
i++;
if (line <= 0) {
sb.append(str).append("\r\n");
} else if (i == line) {
return str.trim();
}
}
sc.close();
} catch (Exception e) {
} finally {
close(sc);
}
return sb.toString();
}
修复后结果如下: macList:[C8-B2-9B-DA-42-B0, 00-50-56-C0-00-08] c1:00aabdc6d979de09f33e0751a5bf93df cpu:178BFBFF00860F01 c2:896dc7fffc815496d9e132ac82f5c7dc harddisk:0025_3885_01B0_F990. c3:60862dccd033fb4ae4fb0337c60525c4 code:00AABDC6D979DE09F33E0751A5BF93DF896DC7FFFC815496D9E132AC82F5C7DC60862DCCD033FB4AE4FB0337C60525C4