将Java控制台输出(逐行)保存到不同的变量?

时间:2012-10-04 04:37:18

标签: java sockets networking

我有一个java代码,它会将关联计算机(localhost除外)的所有IP地址和MAC ID返回到Console。每个IP和关联的MAC ID将以新行显示。我们可以将每一行存储在每个新变量中,例如IP1,MACID1,IP2,MACID2 ......?等待解决方案。提前谢谢。

这是我的代码:

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();
        }
            }
        }
    }

}
}

2 个答案:

答案 0 :(得分:1)

您应该可以随时将每个IP / MAC地址添加到ArrayList。然后,当你完成后,你将把它们全部放在一个地方

答案 1 :(得分: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());
        }
    }
}