import java.io.IOException;
import java.io.InputStream;
public static void main(String[] args) throws IOException, InterruptedException {
String cmdText = "jd.com";
Process process = Runtime.getRuntime().exec("ping "+cmdText);
process.waitFor();
byte[] buffer = new byte[256];
int cnt = 0;
boolean result = false;
InputStream is = process.getInputStream();
while((cnt=is.read(buffer))>=0) {
String s = new String(buffer,0,cnt,"gbk");
System.out.println(new String(buffer,0,cnt,"gbk"));
if(s.contains("来自")){
result = true;
break;
}
}
System.out.println(result);
}
import java.io.InputStream;
/**
*@author lzf
*/
public class Command {public static void main(String[] args) throws IOException, InterruptedException {
String cmdText = "jd.com";
Process process = Runtime.getRuntime().exec("ping "+cmdText);
process.waitFor();
byte[] buffer = new byte[256];
int cnt = 0;
boolean result = false;
InputStream is = process.getInputStream();
while((cnt=is.read(buffer))>=0) {
String s = new String(buffer,0,cnt,"gbk");
System.out.println(new String(buffer,0,cnt,"gbk"));
if(s.contains("来自")){
result = true;
break;
}
}
System.out.println(result);
}
}
这只是其中方法之一,要是命令返回结果乱码你需要设置对应的解码方式