集册 Java实例教程 获取操作系统MAC地址

获取操作系统MAC地址

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

516
获取操作系统MAC地址
// 来自 N o  w  J a v a . c o m - 时  代  Java


//package com.nowjava;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


public class Main {


    public static String getOSMACAddress() {

        String mac = null;

        BufferedReader bufferedReader = null;

        Process process = null;

        try {

            process = Runtime.getRuntime().exec("/sbin/ifconfig en0");

            // linuxeth0? mac

            bufferedReader = new BufferedReader(new InputStreamReader(

                    process.getInputStream(), "utf-8"));

            String line = null;

            int index = -1;

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

                index = line.toLowerCase().indexOf("ether");// ?[hwaddr]// 来自 nowjava.com - 时  代  Java

                if (index >= 0) {// ?

                    mac = line.substring(index + "ether".length() + 1)

                            .trim();

                    // mac?2?

                    break;

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                
展开阅读全文