gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:32:28 25 4
gpt4 key购买 nike

我有一个java代码,它将把关联计算机(本地主机除外)的所有IP地址和MAC ID返回到控制台。每个 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();
}
}
}
}

}
}

最佳答案

使用 map 怎么样?如果是这样,您可以将您的 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());
}
}
}

关于java - 将 Java 控制台输出(逐行)保存到不同的变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12720332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com