java调用系统命令

来源:互联网 发布:易语言砍价源码 编辑:程序博客网 时间:2024/05/22 04:32
import java.io.IOException;
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);
}

}

这只是其中方法之一,要是命令返回结果乱码你需要设置对应的解码方式