import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Test2 {
public static void main(String[] args) throws Exception {
run("ipconfig", true);
}
public static void run(String command,final boolean printLog) {
// System.out.println("执行命令:" + command);
Process process = null ;
try {
process = Runtime.getRuntime().exec(command);
final InputStream is1 = process.getInputStream();
final InputStream is2 = process.getErrorStream();
new Thread() {
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(is1, "gbk"));
String lineB = null;
while ((lineB = br.readLine()) != null) {
if (printLog) {
System.out.println(lineB);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is1!=null){
is1.close();
}
if(br!=null){
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
BufferedReader br2 = null;
try {
br2 = new BufferedReader(new InputStreamReader(is2, "gbk"));
String lineC = null;
while ((lineC = br2.readLine()) != null) {
if (printLog) {
System.out.println(lineC);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is2!=null){
is2.close();
}
if(br2!=null){
br2.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
process.waitFor();
System.out.println("执行命令完成:" + command);
} catch (Throwable e) {
System.out.println("执行命令失败:" + command);
e.printStackTrace();
}
}
}