qiyadeng

          專注于Java示例及教程
          posts - 84, comments - 152, trackbacks - 0, articles - 34

          獲取本地主機(jī)的MAC地址

          Posted on 2005-10-01 23:24 qiyadeng 閱讀(875) 評論(0)  編輯  收藏 所屬分類: J2EE
          前些天,有篇文章,準(zhǔn)確的說是代碼放到csdn的博客上,本是學(xué)習(xí)的一個(gè)筆記,但是既然關(guān)心的人很多,于是再一次把它轉(zhuǎn)貼過了,那個(gè)博客已經(jīng)不再用了。

           1/*
           2 FileName:MACHomework.java
           3 Author:流浪小子
           4 Date:2004-7-5
           5 E-mail:qiyadeng@hotmail.com
           6 Purpose:獲取本地主機(jī)的MAC地址
           7 */

           8import java.io.*;
           9import java.util.*;
          10
          11public class Main {
          12    static private final int MACLength = 18;
          13
          14    public static void main(String args[]) {
          15        System.out.print("本機(jī)的物理地址是:");
          16        System.out.println(getMACAddress());
          17    }

          18
          19    static public String getMACAddress() {
          20        SysCommand syscmd = new SysCommand();
          21        //系統(tǒng)命令
          22        String cmd = "cmd.exe /c ipconfig/all";
          23        Vector result;
          24        result = syscmd.execute(cmd);
          25        return getCmdStr(result.toString());
          26    }

          27
          28    static public String getCmdStr(String outstr) {
          29        String find = "Physical Address. . . . . . . . . :";
          30        int findIndex = outstr.indexOf(find);
          31        if (findIndex == -1{
          32            return "未知錯(cuò)誤!";
          33        }
           else {
          34            return outstr.substring(findIndex + find.length() + 1, findIndex
          35                    + find.length() + MACLength);
          36        }

          37    }

          38}

          39
          40//SysCommand類
          41class SysCommand {
          42    Process p;
          43
          44    public Vector execute(String cmd) {
          45        try {
          46            Start(cmd);
          47            Vector vResult = new Vector();
          48            DataInputStream in = new DataInputStream(p.getInputStream());
          49            BufferedReader myReader = new BufferedReader(new InputStreamReader(
          50                    in));
          51            String line;
          52            do {
          53                line = myReader.readLine();
          54                if (line == null{
          55                    break;
          56                }
           else {
          57                    vResult.addElement(line);
          58                }

          59            }
           while (true);
          60            myReader.close();
          61            return vResult;
          62        }
           catch (Exception e) {
          63            return null;
          64
          65        }

          66
          67    }

          68
          69    public void Start(String cmd) {
          70        try {
          71            if (p != null{
          72                kill();
          73            }

          74            Runtime sys = Runtime.getRuntime();
          75            p = sys.exec(cmd);
          76
          77        }
           catch (Exception e) {
          78
          79        }

          80    }

          81
          82    public void kill() {
          83        if (p != null{
          84            p.destroy();
          85            p = null;
          86        }

          87    }

          88
          89}

          90
          主站蜘蛛池模板: 新平| 新邵县| 新竹市| 永济市| 临沭县| 万源市| 黄大仙区| 崇义县| 长乐市| 瑞安市| 合肥市| 金乡县| 辽阳县| 辽阳市| 康定县| 建德市| 灵武市| 蓝山县| 阳城县| 玉林市| 阳曲县| 高安市| 大化| 金门县| 通城县| 西畴县| 福海县| 崇礼县| 淮阳县| 府谷县| 霍林郭勒市| 射洪县| 云安县| 祁东县| 盐源县| 贺州市| 金寨县| 二手房| 伽师县| 清丰县| 桦南县|