Java中的Ping值

Java中的Ping值,第1张

Java中的Ping值

您可以执行以下 *** 作:

//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);        }    }}

匆匆写成



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561289.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存