YCOE

You Can't stOp mE!

导航

java简单的获取windows系统网卡mac地址

Posted on 2006-03-19 19:35  YCOE  阅读(985)  评论(0编辑  收藏  举报

 

 1package com.youkone.tool;
 2
 3import java.io.BufferedReader;
 4import java.io.InputStreamReader;
 5import java.io.IOException;
 6
 7/**
 8 * <p>Title: </p>
 9 *
10 * <p>Description: </p>
11 *
12 * <p>Copyright: Copyright (c) 2006</p>
13 *
14 * <p>Company: </p>
15 *
16 * @author not attributable
17 * @version 1.0
18 */

19public class MACAddress {
20    public MACAddress() {
21    }

22
23    public static String getMACAddress() {
24
25        String address = "";
26        String os = System.getProperty("os.name");
27        if (os != null && os.startsWith("Windows")) {
28            try {
29                String command = "cmd.exe /c ipconfig /all";
30                Process p = Runtime.getRuntime().exec(command);
31                BufferedReader br =
32                        new BufferedReader(
33                                new InputStreamReader(p.getInputStream()));
34                String line;
35                while ((line = br.readLine()) != null{
36                    if (line.indexOf("Physical Address"> 0{
37                        int index = line.indexOf(":");
38                        index += 2;
39                        address = line.substring(index);
40                        break;
41                    }

42                }

43                br.close();
44                return address.trim();
45            }
 catch (IOException e) {}
46        }

47        return address;
48    }

49
50 
51
52public static void main(String[] args) {
53    System.out.println(""+MACAddress.getMACAddress());
54}

55}

56