Windows の diskpart など、コマンド ライン プロセスと対話する必要があります。問題:input.readLine()
次のサンプルでは、ブロッキング while が発生します。
public static void main(String[] args) throws IOException
{
ProcessBuilder processBuilder = new ProcessBuilder("C:\\Windows\\system32\\diskpart.exe");
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
output = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
// read #1 code position
String line = null;
while((line = input.readLine())!= null)
System.out.println(line);
// code position #2
System.out.println("This line is never executed");
output.write("list disk" + System.lineSeparator());
output.flush(); // important
}
出力 (読み取り #1 コード位置から) は次のとおりです。
Microsoft DiskPart-Version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
Auf Computer: MYPC
これは正しいですが、その後は何も起こりません。たとえば、コード位置 #2
System.out.println("This line is never executed");
到達することはありません。これを修正する理由と方法を誰か教えてもらえますか? ありがとう!
アップデート:
バイトごとに読み取ろうとしてもうまくいかないようですか?):
InputStreamReader input = new InputStreamReader(process.getInputStream());
int mychar = -1;
while((mychar = input.read()) != -1)
System.out.println(mychar);
System.out.println("This line is never executed");