简体   繁体   English

你如何从Java程序连续输入到EXE的控制台?

[英]How do you continuously input from Java program to EXE's console?

I have an EXE file, addOne.exe which continuously gets an integer input from the user on the console (NOT command line parameters) and outputs the integer + 1 onto the console . 我有一个EXE文件addOne.exe ,它不断从控制台上的用户获取整数输入(NOT命令行参数) ,并将整数+ 1输出到控制台。 Sample output is shown below: 示例输出如下所示:

1
2

6
7

29
30
...

I have already made the EXE outputs work, that is, Whenever the EXE outputs text to the console, print that text from the Java program . 我已经使EXE输出工作,也就是说,只要EXE将文本输出到控制台,就从Java程序中打印该文本。 I am trying to: 我在尝试着:

  • Get user input from the Java program using Scanner.nextInt() and input to the EXE as console input 使用Scanner.nextInt()Java程序获取用户输入 ,并作为控制台输入输入到EXE
  • I need to send the input one at a time, which means that simply closing the BufferedWriter stream at the end of the program and inputting the stream into the EXE all at once won't work. 我需要一次发送一个输入,这意味着只需在程序结束时关闭BufferedWriter流并立即将流输入EXE将无法正常工作。

Currently, I have the following code: 目前,我有以下代码:

Process process = Runtime.getRuntime().exec("D:\\addOne.exe");
...
Scanner keyboard = new Scanner(System.in);
String input = "";
OutputStream stdin = process.getOutputStream();
while (!input.equals("exit")) {
    try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin))) {
        input = keyboard.nextLine();
        writer.write(input);
    } catch (IOException ex) {
        Logger.getLogger(CmdLineTest.class.getName()).log(Level.SEVERE,null,ex);
    }
}

However, whenever I run this code, the first iteration of the while loop (the first time I input something) is able to send the input to the EXE, but after the first iteration, the error java.io.IOException: Stream Closed is given. 但是,每当我运行此代码时,while循环的第一次迭代(第一次输入内容)都能够将输入发送到EXE,但是在第一次迭代之后,错误java.io.IOException: Stream Closed is给出。

How can I continue to get user input from a Scanner.nextInt() inside a while loop 如何在while循环中继续从Scanner.nextInt()获取用户输入

Thank you. 谢谢。


Edit: writer.flush() does not seem to input the user input from the Java program to the EXE. 编辑: writer.flush()似乎没有将用户输入从Java程序输入到EXE。


Edit: see my answer below 编辑:请参阅下面的答案

Switch the scope of your try and while statements so that you're only making 1 BufferedWriter . 切换trywhile语句的范围,这样你只能制作1个BufferedWriter When your try-with-resources code block ends, the writer is closed and by extension stdin is as well. 当您的try-with-resources代码块结束时,编写器将被关闭,并且扩展名为stdin Thus no more writers can be made for that object. 因此,不能再为该对象制作作者。

try([resources]) {
    while([condition]) {
        ...
    }
} catch(IOException ex) {
    ...
}

It seems that using writer.flush() inputted the stream into the EXE, but the stream required a writer.newLine() before the input was processed by the EXE, as the console requires the enter key to be pressed after the input. 似乎使用writer.flush()将流输入到EXE中,但是在EXE处理输入之前,流需要writer.newLine(),因为控制台需要在输入后按下回车键。

Here is the working code: 这是工作代码:

while (true) {
    try {
        input = keyboard.nextLine();
        writer.write(input);
        System.out.println("IN: "+input);
        writer.newLine();
        writer.flush();
    } catch (IOException ex) {
        Logger.getLogger(CmdLineTest.class.getName()).log(Level.SEVERE, null, ex);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测用户是否未输入(java) - How do you detect if there's not input from user (java) 你如何用Java连续读取文件? - How do you continuously read a file in Java? 程序崩溃时如何保存“Java控制台”日志? - How do you save the “Java Console” Log when a program crashes? Java:如何连续扫描输入? - Java: How do I continuously scan for input? 如何在Mac上的Terminal中获取Java程序的控制台输入? - How do I get console input for a java program in Terminal on Mac? 程序完成后如何防止控制台关闭? - How do you keep the console from closing after the program is done? 如何连续扫描输入到控制台? - How to continuously scan input into console? 如何从Java Client Server程序中的控制台获取输入 - How to get input from Console in Java Client Server Program 如何在从单独的Java程序调用的Microsoft SQL Server中调试存储过程? - How do you debug a stored procedure in a Microsoft SQL Server that's been called from a seperate java program? 您如何在Java程序中发送电子邮件,以向收件人发送控制台显示的内容? - How do you send an email in a java program that sends the recipient what the console displays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM