[英]In java need to fetch data from command prompt and show on console by following tasks
I'm chan beginner for Java developer one of my Interviewer asked this question on Interview I failed and i want to learn this so please share the knowledge if you know.我是 Java 开发人员的初学者,我的一位面试官在我失败的面试中问了这个问题,我想学习这个,所以如果你知道,请分享知识。 In java need to fetch data from command Prompt and show on console by following tasks
在 java 中需要通过以下任务从命令提示符中获取数据并在控制台上显示
open command prompt run as administrator type ( C:\Windows\system32.netstat -an ) It will show 4 columns like打开命令提示符以管理员身份运行类型(C:\Windows\system32.netstat -an)它将显示 4 列,如
Proto Local Address Foreign Address State)
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:554 0.0.0.0:0 LISTENING
UDP 0.0.0.0:3702 *:*
UDP 0.0.0.0:3702 *:*
UDP 0.0.0.0:3702 *:*
like this more line喜欢这条线
Task 1 Fetch data from command prompt show on console only the TCP details任务 1 从命令提示符获取数据仅在控制台上显示 TCP 详细信息
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:554 0.0.0.0:0 LISTENING
Task 2 Fetch data from command prompt show on console only unique local address from Local Address column任务 2 从命令提示符获取数据仅在控制台上显示本地地址列中的唯一本地地址
0.0.0.0:135
0.0.0.0:445
0.0.0.0:554
0.0.0.0:3702
Task 3 Fetch data from command prompt show on console group by State column and count it任务 3 从 State 列的控制台组上的命令提示符中获取数据并进行计数
LISTENING = 3
I tried like,whats the exact correct methodology我试过了,确切的正确方法是什么
String s1= null;
String s2= null;
InputStream i=Runtime.getRuntime().exec("netstat -an").getInputStream();
Scanner scan=new Scanner(i).useDelimiter("\\"+"\n"+"\n");
s1=scan.hasNext()?scan.next():null;
List<String> l=new ArrayList<String>();
try {
l.add(s1.replaceAll("Active Connections\r\n"
+ "\r\n"
+ " Proto Local Address Foreign Address State", ""));
for(String a:l) {
System.out.println(a);
}
}catch(Exception e){
e.printStackTrace();
}
You can get the output using Process
class, like below:您可以使用
Process
class 获取 output,如下所示:
ProcessBuilder builder = new ProcessBuilder();
builder.command("cmd.exe", "/c", "netstat -an");
Process process = builder.start();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
Then handling that data is really up to you, there are multiple ways to acheive the goals:然后处理这些数据真的取决于你,有多种方法可以实现目标:
List<String[]> output = reader.lines()
.map(line -> line.trim().split("\\s+"))
.collect(Collectors.toList());
List<String> task1 = output.stream()
.filter(line -> line[0].equals("TCP"))
.map(line -> String.join(" ", line))
.collect(Collectors.toList());
List<String> task2 = output.stream()
.filter(line -> line[0].equals("TCP"))
.filter(line -> distinctByValue(line[1]))
.map(line -> String.join(" ", line))
.collect(Collectors.toList());
I know the tasks solution are not perfect.我知道任务解决方案并不完美。 Those are just a quickly written examples to show how to handle the data from
reader
.这些只是一个快速编写的示例,用于展示如何处理来自
reader
的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.