使用java Runtime exec调用ping命令

来源:互联网 发布:网络语言cs是什么意思 编辑:程序博客网 时间:2024/05/29 03:19

不多说,直接上代码:

package test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class PingTest {public static void main(String[] args) {BufferedReader br = null;try {Process p = Runtime.getRuntime().exec("ping www.baidu.com");br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));String line = null;StringBuffer sb = new StringBuffer();while ((line = br.readLine()) != null) {sb.append(line + "\n");}System.out.println(sb.toString());} catch (Exception e) {e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}


1 0
原创粉丝点击