请教如何用JAVA程序设计实现windows中的tracert功能
发布网友
发布时间:2022-05-15 08:01
我来回答
共1个回答
热心网友
时间:2023-10-14 17:59
import java.net.*;
import java.io.*;
public class Whois extends Thread
{
public String traceCmdUnix = "tracert -h 10 ";
private StringBuffer result = null;
private void pingCmd(String command)
{
result = new StringBuffer();
try
{
Process p;
p = Runtime.getRuntime().exec(command);
readResult(p.getInputStream());
p.destroy();
} catch (Exception e)
{
e.printStackTrace();
}
}
private void readResult(InputStream in)
{
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(in));
try
{
while (!this.isInterrupted() && (line = br.readLine()) != null)
{
// this.doResultLine(line+"\n");
System.out.println(line);
}
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
br.close();
} catch (IOException e)
{
}
}
if (this.isInterrupted())
System.out.println("*** Canceld ***");
}
public static void main(String args[]) throws Exception
{
Whois MyObj = new Whois();
MyObj.traceCmdUnix = MyObj.traceCmdUnix + " " + "www.yahoo.com";
MyObj.pingCmd(MyObj.traceCmdUnix);
}
}