利用 java Application 執行 cmd 的語法
這個範例是呼叫 dir C:\\windows, 也就是列出 c:\\windows 下的所有檔案
我稍微修正一下列印每行的回傳值,參考來源是列印每個字元
import java.io.IOException;
import java.io.InputStream;
public class ExecuteDOSCommand {
public static void main(String[] args) {
final String dosCommand = "cmd /c dir /s";
final String location = "C:\\WINDOWS";
try {
final Process process = Runtime.getRuntime().exec(
dosCommand + " " + location);
final InputStream in = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null){
System.out.print(strLine);
}
} catch (IOException e) {
e.printStackTrace();
}
} // end main
} // end class
參考:Execute DOS Commands from a Java Program
沒有留言:
張貼留言