我需要计算来自存储在数组列表中的不同字符串对象的重复单词...
我尝试了以下代码:
import java.io.*;
import java.util.*;
public class Runcmd {
public static void main(String[] args) {
ArrayList arrList=new ArrayList();
try
{
Process pr=Runtime.getRuntime().exec("netstat -an");
BufferedReader rd= new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=rd.readLine();
while(line!=null)
{
arrList.add(line);
line=rd.readLine();
}
Iterator it=arrList.iterator();
while(it.hasNext())
{
String sent=(String)it.next();
System.out.println(sent);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
我得到以下输出:
Active Connections
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:3306 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49152 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49153 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49154 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49155 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49156 0.0.0.0:0 LISTENING
TCP 10.50.170.205:139 0.0.0.0:0 LISTENING
TCP 10.50.170.205:49904 74.125.236.86:443 ESTABLISHED
TCP 10.50.170.205:50044 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50045 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50046 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50047 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50048 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50049 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50050 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50051 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50052 23.201.102.33:80 TIME_WAIT
TCP 10.50.170.205:50053 23.57.214.140:80 TIME_WAIT
TCP [::]:135 [::]:0 LISTENING
TCP [::]:445 [::]:0 LISTENING
TCP [::]:3306 [::]:0 LISTENING
TCP [::]:49152 [::]:0 LISTENING
TCP [::]:49153 [::]:0 LISTENING
TCP [::]:49154 [::]:0 LISTENING
TCP [::]:49155 [::]:0 LISTENING
TCP [::]:49156 [::]:0 LISTENING
UDP 0.0.0.0:5355 *:*
UDP 10.50.170.205:137 *:*
UDP 10.50.170.205:138 *:*
UDP 10.50.170.205:1900 *:*
UDP 127.0.0.1:1900 *:*
UDP 127.0.0.1:49397 *:*
UDP [::1]:1900 *:*
UDP [::1]:49396 *:*
现在,我想获取出现次数的单词列表(LISTENING,TIME-WAIT,ESTABLISHED),例如:
LISTENING : 17
TIME-WAIT : 10
ESTABLISHED : 1
请告诉我要达到上面的想法。
定义一些计数变量并在打印时检查字符串...
while(it.hasNext())
{
String sent=(String)it.next();
System.out.println(sent);
if (sent.indexOf("LISTENING") > 0) listening ++;
if (sent.indexOf("TIME-WAIT") > 0) wait++;
if (sent.indexOf("ESTABLISHED") > 0) established ++;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句