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

    }

}

 

posted @ 2014-04-24 22:12  魔豆  阅读(4361)  评论(0编辑  收藏  举报