java本身实现ping命令比较麻烦,我现在用java来exec ping.exe 然后返回ping远方主机所得的信息,请问如何获取并处理ping命令返回在cmd窗口里的信息?即如何获取这些信息?如何读入?或如何存入一个txt中来分析?Pinging www.cache.split.netease.com [202.108.9.34] with 32 bytes of data:Reply from 202.108.9.34: bytes=32 time=26ms TTL=57
Reply from 202.108.9.34: bytes=32 time=27ms TTL=57
Reply from 202.108.9.34: bytes=32 time=27ms TTL=57
Reply from 202.108.9.34: bytes=32 time=26ms TTL=57Ping statistics for 202.108.9.34:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 26ms, Maximum = 27ms, Average = 26ms
Reply from 202.108.9.34: bytes=32 time=27ms TTL=57
Reply from 202.108.9.34: bytes=32 time=27ms TTL=57
Reply from 202.108.9.34: bytes=32 time=26ms TTL=57Ping statistics for 202.108.9.34:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 26ms, Maximum = 27ms, Average = 26ms
解决方案 »
- java的Syntax error, insert ";" to complete ReturnStatement ,不知道什么原因。
- 问一个字符串解析问题。急,谢谢
- 如何写JTextField 监听text.addActionListener事件
- 我的输入这块 什么问题呀?请赐教!
- File 和FileReader有什么区别
- 我这段代码应该怎么优化呢?如下所示用if判断的话,要遍历所有if判断
- JTable能从数据库里取数据吗???
- 请问IBM的JDK1.4哪里有下载?
- 那里有 for linux Java的虚拟机下载,在线等候
- set转collection报错,哪位大佬可以帮小弟分析下
- 请教,编写一个小程序
- 虚拟机报错呀,很奇怪,同一段代码,在JB下运行没问题,在eclipse下就报错
Process p = Runtime.getRuntime().exec(cmd);
InputStream input = p.getInputStream();
public class hello { /**
* @param args
*/
public static void main(String args[]) throws Exception
{
Process process = Runtime.getRuntime().exec("ping www.cache.split.netease.com");
InputStream standardOutput = process.getInputStream();
int c;
String strOut = "";
while ((c = standardOutput.read()) != -1)
{
strOut += (char)c;
//System.out.print((char)c);
}
System.out.print(strOut);
standardOutput.close();
} }
管道比较简单,不过会有可能获取不全面 :
C:\>ping www.cache.split.netease.com >test.txtC:\>type test.txtPinging www.cache.split.netease.com [220.181.28.51] with 32 bytes of data:Reply from 220.181.28.51: bytes=32 time=11ms TTL=57
Reply from 220.181.28.51: bytes=32 time=13ms TTL=57
Reply from 220.181.28.51: bytes=32 time=30ms TTL=57
Reply from 220.181.28.51: bytes=32 time=11ms TTL=57Ping statistics for 220.181.28.51:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 11ms, Maximum = 30ms, Average = 16ms
Process p = Runtime.getRuntime().exec(cmd);
InputStream input = p.getInputStream();
用 Runtime.getRuntime()能够成功么?? 各位尝试过了么??
*运行在windos命令行(cmd)中的命令
*/
private final static String windowsRunCommand( ) throws IOException
{
Process p = Runtime.getRuntime().exec("ipconfig /all"); // ping...
InputStream stdoutStream = new BufferedInputStream (p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;)
{
int c = stdoutStream.read();
if (c == -1) break;
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}这是我程序中的代码。
你也可以修改一下该函数增加一个字符串参数(命令字符串),也可以正确运行的^_^