以编程方式与Java中的I / O程序进行交互

时间:2015-10-27 19:57:17

标签: java stdout stdin maxima

我正在编写一个利用第三方数学软件“千里马”的程序。该程序是一个命令行界面,因此它可以通过我的Java程序与简单的I / O路由进行通信。我已经想出了如何从Java中运行程序,我已经阅读了很多关于如何重新配置​​System.out以及InputStreams / OutputStreams如何工作的内容,但我无法弄清楚如何执行以下操作(我是什么意思)认为应该是一个非常简单的任务):

  1. 从Java输出Maxima命令(如字符串“5 + 5;”)
  2. 检索Maxima的输出,并从Java代码处理它(比如打印给定的字符串+“blah”)。
  3. 从Java ...
  4. 向Maxima输出另一个命令
  5. - 下面是代码,它将运行Maxima并允许我在Eclipse控制台上与它进行交互

    public static void main(final String[] args) {
    
        // An idea I had for manipulaing how the printstream works.
        // Set the system.out to be a custom Prinstream.
        // final PrintStream interceptor = new Interceptor(origOut);
        // System.setOut(interceptor);
    
        // Run the program:
        final String programLocation = "\"C:\\Program Files (x86)\\Maxima-sbcl-5.37.2\\bin\\maxima.bat\"";
        final ProcessBuilder pb = new ProcessBuilder();
        pb.redirectInput(Redirect.INHERIT); // Inherit I/O
        pb.redirectOutput(Redirect.INHERIT);
        pb.command(programLocation);
    
        try {
            // Start the program and allow it to run in Eclipse's/the program's
            // console.
            pb.start().waitFor();
        } catch (final InterruptedException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    
    }
    

    这允许以下交互方式:Image of Console Interaction

1 个答案:

答案 0 :(得分:0)

感谢来自@RealSkeptic的智慧,我想我在这里找到了解决方案。

关键是构建一个BufferedWriter和一个BufferedReader来与Maxima的I / O进行交互。那就是:

BufferedWriter w = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));

这两行代码创建了缓冲读取器和写入器,可以将数据输入到Maxima,并读取Maxima输出。这是一个(相当冗长)这个方法的用例,我基本上用来做我在问题中提出的问题:

public class TestClass {

public static void main(final String[] args) {
    @SuppressWarnings("unused")
    final TestClass ts = new TestClass();
}

private BufferedWriter w;
private BufferedReader r;

public TestClass() {
    // Start the process using process builder
    final String programLocation = "\"C:\\Program Files (x86)\\Maxima-sbcl-5.37.2\\bin\\maxima.bat\"";
    final ProcessBuilder pb = new ProcessBuilder();
    pb.command(programLocation);
    Process process;
    try {
        process = pb.start();
    } catch (final IOException e) {
        e.printStackTrace();
        process = null;
        // killProgram();
    }

    // Build your own wrappers for communicating with the program.
    w = new BufferedWriter(
            new OutputStreamWriter(process.getOutputStream()));
    r = new BufferedReader(new InputStreamReader(process.getInputStream()));

    // Print the five starting messages.
    printFromBuffer();
    printFromBuffer();
    printFromBuffer();
    printFromBuffer();
    printFromBuffer();

    // Run the following three commands in Maxima
    runCommand("5+5;");
    runCommand("2*65;");
    runCommand("quit();");
}

/**
 * Runs the given string and prints out the returned answer.
 */
private void runCommand(final String s) {
    try {
        w.write(s);
        w.flush();
        printFromBuffer();
        printFromBuffer();
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

private void printFromBuffer() {
    try {
        final String s = r.readLine();

        System.out.println(s + " -blah");

    } catch (final IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}
}