下面是在 Windows10 系统中,使用 Java 获取硬盘序列号示例。代码如下:
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.getHardDiskSerialNumber(); for(String serial : serials) { System.out.println(serial); } } /** * 获取硬盘序列号 * S4Y3WTDV * wmic path win32_physicalmedia get serialnumber * @throws InterruptedException */ private String[] getHardDiskSerialNumber() { List<String> codeList = new ArrayList<>(); Scanner sc = null; try { Process process = Runtime.getRuntime().exec(new String[] { "wmic", "path", "win32_physicalmedia", "get", "serialnumber"}); process.getOutputStream().close(); sc = new Scanner(process.getInputStream()); sc.next(); // SerialNumber while(sc.hasNext()) { codeList.add(sc.next()); } } catch (Exception e) { e.printStackTrace(); } finally { if(null != sc) { sc.close(); } } return codeList.toArray(new String[]{}); } }
运行示例,输出如下:
17137****209