使用来自 Java 的重定向标准输入和标准输出运行外部程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11336157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:41:58  来源:igfitidea点击:

Running external program with redirected stdin and stdout from Java

javaexecstdoutstdin

提问by MattS

I'm trying to running an external program from a Java program and I'm having trouble. Basically what I'd like to do would be this:

我正在尝试从 Java 程序运行外部程序,但遇到了问题。基本上我想做的是这样的:

 Runtime.getRuntime().exec("./extprogram <fileIn >fileOut");

However I've found that that doesn't work - Java apparentls needs to use a Processwith input and output streams and other things which I'm not experienced with.

但是我发现这不起作用 - Java 显然需要使用Process带有输入和输出流以及我没有经验的其他东西。

I've looked at a number of examples across the internet (many of which are from SO), and there doesn't seem to be a simple standard way of doing this, which for someone who doesn't fully understand what's going on, can be quite frustrating.

我查看了互联网上的许多示例(其中许多来自 SO),似乎没有一种简单的标准方法可以做到这一点,对于不完全了解正在发生的事情的人来说,可能会非常令人沮丧。

I'm also having trouble trying to build my own code off the examples of other people's code because generally it seems most other people 1. aren't interested in redirecting stdin, and 2. aren't necessarily redirecting stdoutto a file, but instead to System.out.

我也无法尝试根据其他人的代码示例构建自己的代码,因为通常似乎大多数其他人 1. 对重定向不感兴趣stdin,并且 2. 不一定重定向stdout到文件,而是重定向到System.out.

So, would anyone be able to point me in the direction of any good simple code templates for calling external programs and redirecting stdinand stdout? Thanks.

所以,会有人能够指出我的任何好的简单的代码模板的方向调用外部程序和重定向stdinstdout?谢谢。

回答by corsiKa

You could try something like this:

你可以尝试这样的事情:

ProcessBuilder pb = new ProcessBuilder();
pb.redirectInput(new FileInputStream(new File(infile));
pb.redirectOutput(new FileOutputStream(new File(outfile));
pb.command(cmd);
pb.start().waitFor();

回答by pb2q

If you must use Process, then something like this should work:

如果你必须使用Process,那么这样的事情应该可以工作:

public static void pipeStream(InputStream input, OutputStream output)
   throws IOException
{
   byte buffer[] = new byte[1024];
   int numRead = 0;

   do
   {
      numRead = input.read(buffer);
      output.write(buffer, 0, numRead);
   } while (input.available() > 0);

   output.flush();
}

public static void main(String[] argv)
{
   FileInputStream fileIn = null;
   FileOutputStream fileOut = null;

   OutputStream procIn = null;
   InputStream procOut = null;

   try
   {
      fileIn = new FileInputStream("test.txt");
      fileOut = new FileOutputStream("testOut.txt");

      Process process = Runtime.getRuntime().exec ("/bin/cat");
      procIn = process.getOutputStream();
      procOut = process.getInputStream();

      pipeStream(fileIn, procIn);
      pipeStream(procOut, fileOut);
   }
   catch (IOException ioe)
   {
      System.out.println(ioe);
   }
}

Note:

笔记:

  • Be sure to closethe streams
  • Change this to use buffered streams, I think the raw Input/OutputStreamsimplementation may copy a byte at a time.
  • The handling of the process will probably change depending on your specific process: catis the simplest example with piped I/O.
  • 一定要到close溪流
  • 将此更改为使用缓冲流,我认为原始Input/OutputStreams实现可能一次复制一个字节。
  • 进程的处理可能会根据您的特定进程而改变:cat是管道 I/O 的最简单示例。

回答by Snake

have you tried System.setIn and System.setOut? has been around since JDK 1.0.

你试过 System.setIn 和 System.setOut 吗?自 JDK 1.0 以来一直存在。

public class MyClass
{
    System.setIn( new FileInputStream( "fileIn.txt" ) );
    int oneByte = (char) System.in.read();
    ...

    System.setOut( new FileOutputStream( "fileOut.txt" ) );
    ...