2012-10-04 42 views
0

我有一個java代碼,它將關聯計算機(localhost除外)的所有IP地址和MAC ID都返回給控制檯。每個IP和關聯的MAC ID將以新行顯示。我們可以將每行存儲在每個新變量中,例如IP1,MACID1,IP2,MACID2 ...?等待解決方案。提前致謝。保存Java控制檯輸出(逐行)到不同的變量?

這裏是我的代碼:

import java.net.*; 
import java.util.*; 
import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.UnknownHostException; 

public class NIC { 

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

    List<InetAddress> addrList = new ArrayList<InetAddress>(); 
    Enumeration<NetworkInterface> interfaces = null; 
    try { 
     interfaces = NetworkInterface.getNetworkInterfaces(); 
    } catch (SocketException e) { 
     e.printStackTrace(); 
    } 

    InetAddress localhost = null; 

    try { 
     localhost = InetAddress.getByName("127.0.0.1"); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } 

    while (interfaces.hasMoreElements()) { 
     NetworkInterface ifc = interfaces.nextElement(); 
     Enumeration<InetAddress> addressesOfAnInterface = ifc.getInetAddresses(); 

     while (addressesOfAnInterface.hasMoreElements()) { 
      InetAddress address = addressesOfAnInterface.nextElement(); 

      if (!address.equals(localhost) && !address.toString().contains(":")) { 
       addrList.add(address); 
       //System.out.println("\n"); 
       System.out.println(address.getHostAddress() + "\r"); 
       //System.out.println("\n"); 

try { 
      //InetAddress address = InetAddress.getLocalHost(); 
      InetAddress address1 = InetAddress.getByName(address.getHostAddress()); 

      /* 
      * Get NetworkInterface for the current host and then read 
      * the hardware address. 
      */ 
      NetworkInterface ni = 
        NetworkInterface.getByInetAddress(address1); 
      if (ni != null) { 
       byte[] mac = ni.getHardwareAddress(); 
       if (mac != null) { 
        /* 
        * Extract each array of mac address and convert it 
        * to hexa with the following format 
        * 08-00-27-DC-4A-9E. 
        */ 
        for (int i = 0; i < mac.length; i++) { 

         System.out.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n"); 
        } 
       } else { 
        System.out.println("Address doesn't exist or is not " + 
          "accessible."); 
       } 
      } else { 
       System.out.println("Network Interface for the specified " + 
         "address is not found."); 
      } 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (SocketException e) { 
      e.printStackTrace(); 
     } 
      } 
     } 
    } 

} 
} 
+0

這是列表是。 – gtgaxiola

回答

1

如何使用地圖?如果是這樣,您可以將您的IP地址和MAC ID保存在一起。

Map<String,String> addressMap = new HashMap<String,String>(); 

 String macStr = ""; 
     for (int i = 0; i < mac.length; i++) { 
      System.out.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n"); 
      macStr += String.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n"); 
     } 
     System.out.println("macStr" + macStr); 
     addressMap.put(address.getHostAddress(), macStr); 

迭代地圖。

for (Map.Entry<String, String> entry : addressMap.entrySet()) 
     { 
      System.out.println(entry.getKey() + "/" + entry.getValue()); 
     } 

整個代碼

import java.net.*; 
import java.util.*; 
import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.UnknownHostException; 

public class NIC { 

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

     List<InetAddress> addrList = new ArrayList<InetAddress>(); 
     Map<String,String> addressMap = new HashMap<String,String>(); 

     Enumeration<NetworkInterface> interfaces = null; 
     try { 
      interfaces = NetworkInterface.getNetworkInterfaces(); 
     } catch (SocketException e) { 
      e.printStackTrace(); 
     } 

     InetAddress localhost = null; 

     try { 
      localhost = InetAddress.getByName("127.0.0.1"); 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } 

     while (interfaces.hasMoreElements()) { 
      NetworkInterface ifc = interfaces.nextElement(); 
      Enumeration<InetAddress> addressesOfAnInterface = ifc.getInetAddresses(); 

      while (addressesOfAnInterface.hasMoreElements()) { 
       InetAddress address = addressesOfAnInterface.nextElement(); 

       if (!address.equals(localhost) && !address.toString().contains(":")) { 
        addrList.add(address); 
        //System.out.println("\n"); 
        System.out.println(address.getHostAddress() + "\r"); 
        //System.out.println("\n"); 

        try { 
         //InetAddress address = InetAddress.getLocalHost(); 
         InetAddress address1 = InetAddress.getByName(address.getHostAddress()); 

         /* 
         * Get NetworkInterface for the current host and then read 
         * the hardware address. 
         */ 
         NetworkInterface ni = 
           NetworkInterface.getByInetAddress(address1); 
         if (ni != null) { 
          byte[] mac = ni.getHardwareAddress(); 
          if (mac != null) { 
           /* 
           * Extract each array of mac address and convert it 
           * to hexa with the following format 
           * 08-00-27-DC-4A-9E. 
           */ 

           String macStr = ""; 
           for (int i = 0; i < mac.length; i++) { 
            macStr += String.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n"); 
           } 
           addressMap.put(address.getHostAddress(), macStr); 
          } else { 
           System.out.println("Address doesn't exist or is not " + 
             "accessible."); 
          } 
         } else { 
          System.out.println("Network Interface for the specified " + 
            "address is not found."); 
         } 
        } catch (UnknownHostException e) { 
         e.printStackTrace(); 
        } catch (SocketException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 

     for (Map.Entry<String, String> entry : addressMap.entrySet()) 
     { 
      System.out.println(entry.getKey() + "/" + entry.getValue()); 
     } 
    } 
} 
1

你應該能夠只是每個IP/MAC地址添加到一個ArrayList,當您去。然後當你完成後,你將他們全部在一個地方