java 调用doc下令
java 调用doc命令
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CMDExecRun {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
Process process=Runtime.getRuntime().exec("D:/Program_Files/Java/jdk1.6.0_30/bin/java");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br = new BufferedReader(new InputStreamReader(process.getErrorStream(), "gbk"));
while ((line = br.readLine()) != null) {
//System.err.println(line);
}
// 当前线程等待该process结束,既挂起主线程
process.waitFor();
}
}
======================
把命令写到文件里
=========================================================
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CMDExecRun {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
String str="testrun.bat";
Process process=Runtime.getRuntime().exec(str);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br = new BufferedReader(new InputStreamReader(process.getErrorStream(), "gbk"));
while ((line = br.readLine()) != null) {
//System.err.println(line);
}
//等待子进程完成再往下执行。
process.waitFor();
}
}