Java Mac Address Get getMacAddress(String separator)

Here you can find the source of getMacAddress(String separator)

Description

get Mac Address

License

Open Source License

Declaration

public static String getMacAddress(String separator) throws SocketException, UnknownHostException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;

import java.net.NetworkInterface;
import java.net.SocketException;

import java.net.UnknownHostException;

public class Main {
    public static String getMacAddress() throws SocketException, UnknownHostException {
        return getMacAddress(':');
    }/* w  w w.j  a  v  a 2  s.  c o m*/

    public static String getMacAddress(char separator) throws SocketException, UnknownHostException {
        return getMacAddress(String.valueOf(separator));
    }

    public static String getMacAddress(String separator) throws SocketException, UnknownHostException {
        final StringBuilder macAddress = new StringBuilder();

        final InetAddress localHost = InetAddress.getLocalHost();
        final NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

        if (networkInterface == null) {
            return null;
        }

        for (byte b : networkInterface.getHardwareAddress()) {
            macAddress.append(String.format("%02X", b)).append(separator);
        }

        return macAddress.substring(0, macAddress.length() - separator.length());
    }
}

Related

  1. getMacAddress()
  2. getMacAddress()
  3. getMACAddress()
  4. getMacAddress(String host)
  5. getMACAddress(String interfaceName)
  6. getMacAddressBytes()
  7. getMacAddresses()
  8. getMacAddressString()
  9. getMacAddrs()