您可以执行以下 *** 作:
//The command to executeString pingCmd = "ping " + ip + " -t";//get the runtime to execute the commandRuntime runtime = Runtime.getRuntime();Process process = runtime.exec(pingCmd);//Gets the inputstream to read the output of the commandBufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));//reads the outputsString inputLine = in.readLine();while ((inputLine != null)) { if (inputLine.length() > 0) { ........ } inputLine = in.readLine();}
更新: 根据您的需要
public class PingDemo { public static void main(String[] args) { String ip = "localhost"; String time = ""; //The command to execute String pingCmd = "ping " + ip; //get the runtime to execute the command Runtime runtime = Runtime.getRuntime(); try { Process process = runtime.exec(pingCmd); //Gets the inputstream to read the output of the command BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); //reads the outputs String inputLine = in.readLine(); while ((inputLine != null)) { if (inputLine.length() > 0 && inputLine.contains("time")) { time = inputLine.substring(inputLine.indexOf("time")); break; } inputLine = in.readLine(); } System.out.println("time --> " + time); } catch (Exception ex) { System.out.println(ex); } }}
匆匆写成。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)