代碼如下:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Example1 { public static String getHdSerialInfo() { String line = ""; String HdSerial = "";//定義變量 硬盤序列號 try { Process proces = Runtime.getRuntime().exec("cmd /c dir c:");//獲取命令行參數 BufferedReader buffreader = new BufferedReader(new InputStreamReader(proces.getInputStream(),"gbk")); while ((line = buffreader.readLine()) != null) { if (line.indexOf("卷的序列號是 ") != -1) { //讀取參數並獲取硬盤序列號 HdSerial = line.substring(line.indexOf("卷的序列號是 ") + "卷的序列號是 ".length(), line.length()); break; } } } catch (IOException e) { e.printStackTrace(); } return HdSerial; } /** * @param args */ public static void main(String[] args) { System.out.println(getHdSerialInfo()); } }