package com.bochy;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
class PrintThread implements Runnable {
private InputStream _is = null;
private BufferedReader _reader = null;
public PrintThread(InputStream is) {
this._is = is;
}
public void run() {
try {
while (this != null) {
_reader = new BufferedReader(new InputStreamReader(_is, "GBK"));
String _line = null;
while ((_line = _reader.readLine()) != null) {
System.out.println(_line);
}
_is.close();
_reader.close();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Ping {
public static void main(String[] agrs) {
Process process;
try {
process = Runtime.getRuntime().exec("ping 127.1.1.1");
Thread thread = new Thread(
new PrintThread(process.getInputStream()));
Thread thread_1 = new Thread(new PrintThread(
process.getErrorStream()));
thread.start();
thread_1.start();
process.getOutputStream().flush();
process.getOutputStream().close();
if (process.waitFor() != 0) {
if (process.exitValue() == 1) {
System.out.println("命令执行失败!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ping
最新推荐文章于 2022-01-04 11:57:48 发布