想定シナリオ:
ファイルからコマンドを取得して実行し、結果をファイルに書き込む
import java.io.*;
public class a {
public static void main(String[] args) throws IOException{
// 1. テスト用ファイル作成
FileOutputStream stream = new FileOutputStream("./a.txt");
OutputStreamWriter writer = new OutputStreamWriter(stream);
writer.append("cmd /c type nul > a.log"+"\r\n");
writer.append("cmd /c mysql -uroot -p\"password\" -D test --skip-column-names -e \"select now();\" >> a.log"+"\r\n");
writer.append("cmd /c mysql -uroot -p\"password\" -D test --skip-column-names -e \"show tables;\" >> a.log"+"\r\n");
writer.append("cmd /c mysql -uroot -p\"password\" -D test --skip-column-names -e \"show create table tab1 \\G\" >> a.log"+"\r\n");
writer.close();
stream.close();
// 2. OSコマンド実行
try {
FileReader fr = new FileReader("./a.txt");
BufferedReader reader = new BufferedReader(fr);
String line;
Runtime runtime = Runtime.getRuntime();
Process p ;
while ( (line = reader.readLine() ) != null) {
p = runtime.exec(line);
p.waitFor();
p.destroy();
}
reader.close();
fr.close();
} catch (Exception e) {
System.out.println(e);
}
}
}