本文介绍了如何在Java中将EOF发送到进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java程序中运行 groff 。输入来自字符串。在真实的命令行中,我们将在Linux / Mac中以 ^ D 终止输入。那么如何在Java程序中发送该终结符呢?

I want to run groff in a Java program. The input comes from a string. In real command line, we will terminate the input by ^D in Linux/Mac. So how to send this terminator in Java program?

String usage +=
    ".Dd \\[year]\n"+
    ".Dt test 1\n"+
    ".Os\n"+
    ".Sh test\n"+
    "^D\n";    // <--- EOF here?
Process groff = Runtime.getRuntime().exec("groff -mandoc -T ascii -");
groff.getOutputStream().write(usage.getBytes());
byte[] buffer = new byte[1024];
groff.getInputStream().read(buffer);
String s = new String(buffer);
System.out.println(s);

还是其他想法?

推荐答案

^ D 不是字符;这是一个由外壳程序解释的命令,告诉它关闭流到进程(因此进程在 stdin 上接收到EOF)。

^D isn't a character; it's a command interpreted by your shell telling it to close the stream to the process (thus the process receives EOF on stdin).

您需要在代码中执行相同的操作;刷新并关闭 OutputStream

You need to do the same in your code; flush and close the OutputStream:

String usage =
  ".Dd \\[year]\n" +
  ".Dt test 1\n" +
  ".Os\n" +
  ".Sh test\n";
...
OutputStream out = groff.getOutputStream();
out.write(usage.getBytes());
out.close();
...

这篇关于如何在Java中将EOF发送到进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:22