【发布时间】:2017-12-27 07:15:20
【问题描述】:
这是我的代码,它告诉 IP 地址的 PING 状态。但是,我不能让它使用 printstream 附加输出,而不是使用这么多的 ip 地址,我希望使用 Loop,这样我就不能只使用一次。 一点帮助将不胜感激。
PrintStream out;
out = new PrintStream(new FileOutputStream("output1.csv"));
System.setOut(out);
String ipAddress = "172.20.10.13";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(1000) ? "Host is reachable" : "Host is NOT reachable");
out = new PrintStream(new FileOutputStream("output7.csv"));
System.setOut(out);
ipAddress = "192.168.1.10";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(1000) ? "Host is reachable" : "Host is NOT reachable");
out = new PrintStream(new FileOutputStream("output10.csv"));
System.setOut(out);
ipAddress = "192.168.1.35";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(1000) ? "Host is reachable" : "Host is NOT reachable");
【问题讨论】:
-
请在您的问题中添加输出和预期输出。
-
您能否说得更具体一些。 “我无法让它使用 printstream 附加输出”到底是什么意思?你到底想要什么?将所有数据写入同一个输出文件(
output.csv而不是output1.csv、output7.csv和output10.csv)? -
只需创建一个函数,将 ipAddress 等内容作为参数传递
标签: java loops netbeans append