外部ファイルコンソール実行
// プロセス定義
ProcessBuilder pb = new ProcessBuilder(
"cmd",
"/C",
"findstr /c:\"aaa\" /c:\"ccc\" c:\\target.txt",
">",
"c:\\result.txt"
);
// プロセス実体
Process p = null;
try {
// プロセス開始
p = pb.start();
// 結果ストリーム取得用
BufferedReader br = null;
// 標準ストリーム取得
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
try {
String line;
while((line = br.readLine()) != null) {
if(line.trim().length() != 0) System.out.println(line);
}
} catch(IOException e) {
throw e;
} finally {
try { if(br != null) br.close(); } catch(IOException e) {}
}
// エラーストリーム取得
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
try {
String line;
while((line = br.readLine()) != null) {
if(line.trim().length() != 0) System.out.println(line);
}
} catch(IOException e) {
throw e;
} finally {
try { if(br != null) br.close(); } catch(IOException e) {}
}
} catch(IOException e) {
e.printStackTrace();
}
//プロセスの終了待ち
try {
p.waitFor();
} catch(InterruptedException e) {
e.printStackTrace();
}
// 破棄
try { InputStream is = p.getInputStream(); if(is != null) is.close(); } catch(IOException e) {}
try { InputStream es = p.getErrorStream(); if(es != null) es.close(); } catch(IOException e) {}
try { OutputStream os = p.getOutputStream(); if(os != null) os.close(); } catch(IOException e) {}
p.destroy();