是否将Java控制台输出(逐行)保存到不同的变量?
我有一个java代码,它将把相关计算机(本地主机除外)的所有IP地址和MAC ID返回控制台。每个IP和相关的MAC ID将显示在新行中。我们能在每一个新变量中存储每一行吗,比如IP1,MACID1,IP2,MACID2?等待解决。提前谢谢 这是我的密码:是否将Java控制台输出(逐行)保存到不同的变量?,java,sockets,networking,Java,Sockets,Networking,我有一个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.Soc
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();
}
}
}
}
}
}
import java.net.*;
导入java.util.*;
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.net.UnknownHostException;
公共类网卡{
公共静态void main(字符串args[])引发异常{
List addrList=new ArrayList();
枚举接口=null;
试一试{
interfaces=NetworkInterface.getNetworkInterfaces();
}捕获(SocketException e){
e、 printStackTrace();
}
InetAddress localhost=null;
试一试{
localhost=InetAddress.getByName(“127.0.0.1”);
}捕获(未知后异常e){
e、 printStackTrace();
}
while(interfaces.hasMoreElements()){
NetworkInterface ifc=interfaces.nextElement();
枚举地址esofanInterface=ifc.getInetAddresses();
while(addressesOfAnInterface.hasMoreElements()){
InetAddress=addressesOfAnInterface.nextElement();
如果(!address.equals(localhost)&&!address.toString()包含(“:”){
addrList.add(地址);
//System.out.println(“\n”);
System.out.println(address.getHostAddress()+“\r”);
//System.out.println(“\n”);
试一试{
//InetAddress=InetAddress.getLocalHost();
inetAddress1=InetAddress.getByName(address.getHostAddress());
/*
*获取当前主机的NetworkInterface,然后读取
*硬件地址。
*/
网络接口ni=
NetworkInterface.getByInetAddress(address1);
如果(ni!=null){
字节[]mac=ni.getHardwareAddress();
如果(mac!=null){
/*
*提取每个mac地址数组并将其转换
*使用以下格式转换为hexa
*08-00-27-DC-4A-9E。
*/
for(int i=0;i
您应该能够在运行时将每个IP/MAC地址添加到ArrayList中。然后,当您完成后,您将在一个地方拥有所有地图如何使用地图??如果是这样,您可以同时保存您的IP地址和MAC ID
Map<String,String> addressMap = new HashMap<String,String>();
Map addressMap=newhashmap();
String macStr=”“;
for(int i=0;i
迭代映射
for (Map.Entry<String, String> entry : addressMap.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
for(Map.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());
}
}
}
import java.net.*;
导入java.util.*;
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.net.UnknownHostException;
公共类网卡{
公共静态void main(字符串args[])引发异常{
List addrList=new ArrayList();
Map addressMap=newhashmap();
枚举接口=null;
试一试{
interfaces=NetworkInterface.getNetworkInterfaces();
}捕获(SocketException e){
e、 printStackTrace();
}
InetAddress localhost=null;
试一试{
localhost=InetAddress.getByName(“127.0.0.1”);
}捕获(未知后异常e){
e、 printStackTrace();
}
while(interfaces.hasMoreElements()){
NetworkInterface ifc=interfaces.nextElement();
枚举地址esofanInterface=ifc.getInetAddresses();
while(addressesOfAnInterface.hasMoreElements()){
InetAddress=addressesOfAnInterface.nextElement();
如果(!address.equals(localhost)&&!address.toString()包含(“:”){
addrList.add(地址);
//System.out.println(“\n”);
System.out.println(address.getHostAddress()+“\r”);
//System.out.println(“\n”);
试一试{
//InetAddress=InetAddress.getLocalHost();
inetAddress1=InetAddress.getByName(address.getHostAddress());
/*
*获取当前主机的NetworkInterface,然后读取
*硬件地址。
*/
网络接口ni=
NetworkInterface.getByInetAddress(address1);
如果(ni!=null){
字节[]mac=ni.getHardwareAddress();
如果(mac!=null){
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());
}
}
}