【发布时间】:2012-05-21 10:02:09
【问题描述】:
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream(
new FileOutputStream("/home/main/smt/output/out.txt")));
try {
String line;
Process p = Runtime.getRuntime().exec(
"/home/main/smt/tools/moses-2010-08-13/moses/moses-cmd/src/moses " +
"-f /home/main/smt/work/model/moses.ini " +
"< /home/main/smt/work/corpus/dataset.en" );
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
catch (Exception e) {
// ...
}
}
命令
home/main/smt/tools/moses-2010-08-13/moses/moses-cmd/src/moses
-f /home/main/smt/work/model/moses.ini
< /home/main/smt/work/corpus/dataset.en
>/home/main/smt/output/out.txt
在 Linux 的终端中执行并创建 out.txt。但在 java 中没有创建 out.txt。
dataset.en 是输入文件。使用模型中src 和moses.ini 中的exe moses,dataset.en 中的内容被翻译并保存在out.txt 中。
但是在运行此代码时,这里没有创建 out.txt。尽管控制台中没有显示任何内容,但我从命令中删除了将输出保存在文件中。如果我更改 Process p = Runtime.getRuntime().exec(ls) 它工作正常。
【问题讨论】:
-
重定向运算符,如
<和>是shell 的一部分,不能直接与Runtime.exec()一起使用。选项见this SO thread