集册 Java实例教程 获取Windows MAC地址

获取Windows MAC地址

欢马劈雪     最近更新时间:2020-01-02 10:19:05

487
获取Windows MAC地址


//package com.nowjava;/** 来自 n o w j a v a . c o m - 时代Java**/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


public class Main {

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

        System.out.println(getWindowsMACAddress());

    }


    public static String getWindowsMACAddress() {

        String mac = null;

        BufferedReader bufferedReader = null;

        Process process = null;

        try {

            process = Runtime.getRuntime().exec("ipconfig /all");

            bufferedReader = new BufferedReader(new InputStreamReader(

                    process.getInputStream()));
                    /* from 
                    nowjava - 时  代  Java*/

            String line = null;

            int index = -1;

            while ((line = bufferedReader.readLine()) != null) {

                index = line.toLowerCase().indexOf("physical address");

                if (index != -1) {

                    index = line.indexOf(":");

                    if (index != -1) {

                        mac = line.substring(index + 1).trim();

                    }

                    break;

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                
展开阅读全文