java獲取硬盤序列號(windows系統)


代碼如下:

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());

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM