转载

java调用cmd命令ping

  1. public class Test { public static void main(String[] args) { String ipAddress = "127.0.0.1"; try { ping(ipAddress); } catch (IOException e) { e.printStackTrace(); } } private static boolean ping(String ipAddress) throws IOException { //exec中的字符串可以替换成其他的cmd命令,没有详细测试过-- Process pro = Runtime.getRuntime().exec("ping " + ipAddress + " -n 4"); BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream())); String line = ""; while ((line = buf.readLine()) != null) { if (line.toLowerCase().indexOf("reply from")!= -1) { return true; } } return false; } }

第一次在网上分享点东西,内容很简单,希望有朋友能用得上。

 

正文到此结束
Loading...