java程序中的cmd输出不完整
在tree命令中,我在程序中得到不完整的输出。谁能帮我修一下吗java程序中的cmd输出不完整,java,cmd,output,Java,Cmd,Output,在tree命令中,我在程序中得到不完整的输出。谁能帮我修一下吗 public static void main(String[] args) { for (;;) { try { String command = JOptionPane.showInputDialog("> "); Process p = Runtime.getRuntime().exec("cmd /C " + command);
public static void main(String[] args) {
for (;;) {
try {
String command = JOptionPane.showInputDialog("> ");
Process p = Runtime.getRuntime().exec("cmd /C " + command);
Scanner sc = new Scanner(p.getInputStream());
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
if ("exit".equals(command)) {
System.exit(0);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
我在您的代码中看到的唯一可能显而易见的事情是,您的命令输入将使用一个且仅使用一个命令,您可能希望这样刷新
while (sc.hasNext()) {
System.out.println(sc.nextLine());
System.out.flush(); // <-- To get all your output, before exit?
}
预期结果和实际结果?这里有问题吗?@JavanshirMammadov这是另一个问题。但是您可以使用围绕ByteArrayOutputStream的PrintWriter,而不是System.out。