简体   繁体   English

Java重定向输入和输出流

[英]Java redirect input and output stream

To excecute SENNA in the terminal I use the command: 要在终端中执行SENNA ,请使用以下命令:

senna.exe < input.txt > result.txt

Now I want to realize this in a java program. 现在,我想在Java程序中实现这一点。 This is my code so far 到目前为止,这是我的代码

ProcessBuilder builder = new ProcessBuilder("senna.exe");
builder.redirectErrorStream(true);
Process process = builder.start();
OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();

BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

writer.write("This is a test sentence");;
writer.flush();
String line;
while ((line = reader.readLine ()) != null) {
    System.out.println ("Stdout: " + line);
}

To redirect the input, output and error stream I used the code from this thread . 为了重定向输入,输出和错误流,我使用了该线程中的代码。 The problem is that I get the following error message: 问题是我收到以下错误消息:

FATAL ERROR: unable to open file hash/words.lst 严重错误:无法打开文件hash / words.lst

Am I doing something wrong? 难道我做错了什么?

From the examples you've given it seems that you're adapting Linux code from this thread to run on Windows with senna.exe. 从给出的示例中,您似乎正在改编此线程中的 Linux代码以使其在Windows上使用senna.exe运行。

From the error you're getting it seems that you forgot to change Linux's forward slash (/) to Windows' backslash (\\). 从错误中您可能会发现似乎忘记了将Linux的正斜杠(/)更改为Windows的反斜杠(\\)。

Try changing your filepath's forwardslash to backslash. 尝试将文件路径的正斜杠更改为反斜杠。

As far as I can see, you haven't set the directory path to ProcessBuilder object. 据我所知,您尚未将目录路径设置为ProcessBuilder对象。 This error seems to be because there is a folder called 'hash' in senna folder which can't be reached. 该错误似乎是由于无法访问的番泻叶文件夹中有一个名为“哈希”的文件夹。

Please try this: builder.directory(new File("/yourpathtosenna/senna/")); 请尝试以下操作:builder.directory(new File(“ / yourpathtosenna / senna /”)); (I am on a linux machine) (我在linux机器上)

Your error should most probably change, but I am not sure if you will get the output as I am also struggling with running senna interactively through Java on a Linux machine at the moment. 您的错误很可能应该改变,但是我不确定您是否会得到输出,因为我现在也正在努力通过Linux在Java机器上以交互方式运行番泻叶。

Good luck and do update if you are successful! 祝您好运,如果成功,请及时更新!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM