使用java获取计算机中硬盘的真实序列号

时间:2014-01-19 15:51:06

标签: java hard-drive serial-number

我一直在尝试这段代码,但我想知道输出不是序列号(SN)。结果只包含没有字母的数字。我想知道,它必须是数字和字母之间的组合。

以下是代码:

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class DiskUtils {
    private DiskUtils() {  }

    public static String getSerialNumber(String drive) {
        String result = "";
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);

            String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                        +"Set colDrives = objFSO.Drives\n"
                        +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
                        +"Wscript.Echo objDrive.SerialNumber";  // see note
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
            BufferedReader input =
              new BufferedReader
                (new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
               result += line;
            }
            input.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return result.trim();
    }

    public static void main(String[] args){
      String sn = DiskUtils.getSerialNumber("C");
      javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
           null, sn, "Serial Number of C:",
           javax.swing.JOptionPane.DEFAULT_OPTION);
    }
}

然后,我也试图证明真实的SN可能是真的(包含数字和字母)。 这是我要检查硬盘的真实SN的教程:

  1. 打开命令提示符窗口(“开始”菜单>所有程序>附件>命令提示符)。
  2. 输入:vol。
  3. 卷序列号是磁盘序列号。
  4. 代码和教程之间的结果确实有很大不同。 你们知道吗,哪一个是真的?以及如何证明这一点?

3 个答案:

答案 0 :(得分:1)

您可能希望尝试更多内容:

import java.io.*;

public class SerialNumber
{
  public String executeVolCommand()
  {
    String NEWLINE = System.getProperty("line.separator");
    StringBuffer buffer = new StringBuffer();
    try{

      Process pb = new ProcessBuilder("cmd","/c", "vol").start();  
      InputStream in = pb.getInputStream();  
      BufferedReader br = new BufferedReader(new InputStreamReader(in));  
      String line;  
      while ((line = br.readLine()) != null) {  
        buffer.append(line + NEWLINE);  
      }
    }
    catch(Exception e){e.printStackTrace();}
    return buffer.toString();      }
}

然后,一旦得到结果,就可以解析它的实际序列号。希望这会有所帮助。

答案 1 :(得分:0)

如果你还想要答案......

import java.io.*;
import java.util.*;

public class SerialNumber {

public static void main ( String[] args ) throws Exception {

        Process p = Runtime.getRuntime().exec ( "ioreg -l" );
        InputStream is = p.getInputStream();
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while ( ( i = is.read() ) != -1 ) if ( ( char ) i != '|' && ( char ) i != ' ' ) sb.append ( ( char ) i );
        InputStream fis = new ByteArrayInputStream ( sb.toString().getBytes() );
        Properties pro = new Properties();
        pro.load ( fis );
        System.out.println ( pro.get ( "\"IOPlatformSerialNumber\"" ).toString().substring ( 1, pro.get ( "\"IOPlatformSerialNumber\"" ).toString().length() - 1 ) );

    }

} 

我正在运行 Mac OSX 10.10

答案 2 :(得分:0)

 // InputStream stream = Runtime.getRuntime().exec("wmic diskdrive get name,size,model,SerialNumber ").getInputStream();
    List<String> collect;
    InputStream stream = Runtime.getRuntime().exec("wmic diskdrive get SerialNumber ").getInputStream();

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {

     collect  = reader.lines().collect(Collectors.toList());
        /*while ((line = reader.readLine()) != null) {
        model += line;
        }*/
    }

注意:BufferedReader类为您提供了几种从输入流获取信息的方法