以编程方式与Java中的I/O程序交互
我正在写一个利用第三方数学软件“Maxima”的程序。这个程序是一个命令行接口,因此它可以通过我的Java程序通过简单的I/O路由进行通信。我已经了解了如何在Java中运行该程序,并且已经阅读了很多关于如何重新配置System.out以及InputStreams/OutputStreams如何工作的内容,但我不知道如何执行以下操作(我认为这应该是一项非常简单的任务):以编程方式与Java中的I/O程序交互,java,stdout,stdin,maxima,Java,Stdout,Stdin,Maxima,我正在写一个利用第三方数学软件“Maxima”的程序。这个程序是一个命令行接口,因此它可以通过我的Java程序通过简单的I/O路由进行通信。我已经了解了如何在Java中运行该程序,并且已经阅读了很多关于如何重新配置System.out以及InputStreams/OutputStreams如何工作的内容,但我不知道如何执行以下操作(我认为这应该是一项非常简单的任务): 从Java输出到Maxima命令(如字符串“5+5;”) 检索Maxima的输出,并从Java代码中处理它(比如打印给定的字符串
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();
}
}
这就允许了以下互动方式:多亏@realdupoint的智慧之言,我想我在这里找到了一个解决方案 关键是构建一个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();
}
}
}
多亏@realponsibilist的智慧之言,我想我在这里找到了一个解决方案 关键是构建一个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();
}
}
}
您不应该重定向输入和输出。默认设置是能够获取进程的输出、输入和错误流,并能够对它们进行写入和读取-通过重定向,您将失去这种能力。@realpoint,那么我应该使用类似
BufferedReader r=new BufferedReader(new InputStreamReader(process.getInputStream())
要创建一个可以“读取”Maxima输出的工具?好的,我现在正在使用这个过程开发一个解决方案,如果我能让它工作,我一定会将我的结果作为答案发布。谢谢您的帮助。您不应该重定向输入和输出。默认设置是能够获取进程的输出、输入和错误流,并能够对它们进行写入和读取-通过重定向,您将失去这种能力。@realpoint,那么我应该使用类似BufferedReader r=new BufferedReader(new InputStreamReader(process.getInputStream())
要创建一个可以“读取”Maxima输出的工具?好的,我现在正在使用这个过程开发一个解决方案,如果我能让它工作,我一定会将我的结果作为答案发布。谢谢你的帮助。