(Java)Runtime.exec中的文件重定向(双向)? [英] (Java) File redirection (both ways) within Runtime.exec?

查看:369
本文介绍了(Java)Runtime.exec中的文件重定向(双向)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行此命令:

/ceplinux_work3/myName/opt/myCompany/ourProduct/bin/EXECUTE_THIS -p cepamd64linux.myCompany.com:19021/ws1/project_name < /ceplinux_work3/myName/stressting/Publisher/uploadable/00000.bin >> /ceplinux_work3/myName/stressting/Publisher/stats/ws1.project_name.19021/2011-07-22T12-45-20_PID-2237/out.up

但它不起作用,因为EXECUTE_THIS需要通过重定向输入文件,只是将此命令传递给Runtime.exec不起作用。

But it doesn't work because EXECUTE_THIS requires an input file via redirect, and simply passing this command to Runtime.exec doesn't work.

旁注:在来到这里之前,我搜遍了如何解决这个问题。关于Runtime.exec和输入/输出重定向,网上有很多问题/文章。但是,我找不到将文件传递给命令并将结果输出到另一个文件的任何处理。另外,我对输入/输出流完全不熟悉,因此我很难将所有信息放在一起以适应我的具体情况。

Side note: I searched all over on how to solve this before coming here to ask. There are many questions/articles on the web regarding Runtime.exec and Input/Output redirect. However, I cannot find any that deal with passing a file to a command and outputting the result to another file. Plus, I am totally unfamiliar with Input/Output streams, so I have a hard time putting all the info out there together for my specific situation.

那说,任何帮助非常感谢。

That said, any help is much appreciated.

PS如果有多种方法可以做到这一点,我更喜欢吞吐量方面最快的东西。

P.S. If there are multiple ways to do this, I prefer whatever is fastest in terms of throughput.

编辑:正如我在上一个问题中所讨论的,我不能将此更改为bash调用,因为程序必须等待此过程完成才能继续。

As discussed in my last question, I CANNOT change this to a bash call because the program must wait for this process to finish before proceeding.

推荐答案

除非您要发送文件 name 到过程的标准输入,没有区分数据是来自文件还是来自任何其他数据源。

Unless you are sending a file name to the standard input of the process, there is no distinction of whether the data came from a file or from any other data source.

你需要写入由 Process.getOutputStream()给出的 OutputStream 。您写入的数据可以使用 FileInputStream 从文件中读取

You need to write to the OutputStream given by Process.getOutputStream(). The data you write to it you can read in from a file using a FileInputStream.

将它们放在一起可能看起来像这样:

Putting that together might look something like this:

    Process proc = Runtime.getRuntime().exec("...");

    OutputStream standardInputOfChildProcess = proc.getOutputStream();
    InputStream dataFromFile = new FileInputStream("theFileWithTheData.dat");

    byte[] buff = new byte[1024];
    for ( int count = -1; (count = dataFromFile.read(buff)) != -1; ) {
        standardInputOfChildProcess.write(buff, 0, count);
    }

我遗漏了很多细节,这只是为了得到它的要点。你想要安全地关闭东西,可能想考虑缓冲,你需要担心 的陷阱Runtime.exec()

I've left out a lot of details, this is just to get the gist of it. You'll want to safely close things, might want to consider buffering and you need to worry about the pitfalls of Runtime.exec().

将输出写入文件是类似的。获取指向输出文件的 FileOutputStream ,并将从 Process.getInputStream()读取的数据写入 C $ C>的OutputStream 。这里的主要警告是你必须在第二个线程中执行此操作,因为从同一个线程访问两个阻塞流将导致死锁(请参阅上面的文章)。

Writing the output to a file is similar. Obtain a FileOutputStream pointing to the output file and write the data you read from Process.getInputStream() to that OutputStream. The major caveat here is that you must do this operation in a second thread, since accessing two blocking streams from the same thread will lead to deadlock (see the article above).

这篇关于(Java)Runtime.exec中的文件重定向(双向)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆