private static void runner() {
try
{
Runtime rt = Runtime.getRuntime();
//Runtime rt = Runtime();
//rt.getRuntime();
String command = "C:\\dummy.exe";
//command = "cmd";
Process pr = rt.exec(command);
BufferedReader processOutput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
String commandToSend;
//commandToSend = "dir\n";
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a command");
commandToSend = reader.nextLine(); // Scans the next token of the input as an int.
System.out.println(commandToSend);
processInput.write(commandToSend+"\r\n");
processInput.flush();
int lineCounter = 0;
String line ="";
boolean go2 =true;
while(go2)
{
line = processOutput.readLine();
//System.out.println("2");
if(line == null) {
System.out.println("\n\ndone\n");
go2=false;
}
//System.out.println(go2);
System.out.println(++lineCounter + ": " + line);
}
}
reader.close();
processInput.close();
processOutput.close();
pr.waitFor();
System.out.println("done\n");
}
catch(Exception x)
{
x.printStackTrace();
}
}
这是输出
Enter a command
dir
dir
3:
12: 02/03/2018 02:35 PM 301 .classpath
13: 02/03/2018 02:35 PM 382 .project
14: 02/03/2018 02:35 PM <DIR> .settings
15:
16: 02/03/2018 02:36 PM <DIR> src
17: 2 File(s) 683 bytes
18: 5 Dir(s) 702,993,940,480 bytes free
19:
它冻结在这里^ 它在线条处冻结= processOutput.readline() 我尝试了多个打印语句,发现在第19行之后它会冻结,但不会引发错误。 我认为这个问题必须解决缓冲空值的问题 我已经剪了一些解释数字差距的输出 但要说明的主要内容是它不会冻结换行符 就在最后