使用 Java 将命令行输出写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11650042/
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 05:55:25  来源:igfitidea点击:

Writing command line output to a file with in Java

javacommand-line

提问by Michael A

I am running a command line command from java:

我正在从 java 运行命令行命令:

ping localhost > output.txt

The command is send via a Java like this:

该命令通过 Java 发送,如下所示:

Process pr = rt.exec(command);

For some reason the file is not created, but when i run this command from the command line itself, the file does create and the output is in that file.

由于某种原因,文件没有被创建,但是当我从命令行本身运行这个命令时,文件确实创建了并且输出在那个文件中。

Why doesn't the java command create the file?

为什么 java 命令不创建文件?

回答by Andy Thomas

Because you haven't directed it to a file.

因为你还没有将它定向到一个文件。

On the command line, you've requested that it be redirected to a file. You have to do the same thing in Java, via the InputStream provided by the Process object (which corresponds to the output stream of the actual process).

在命令行上,您已请求将其重定向到一个文件。您必须在 Java 中通过 Process 对象(对应于实际进程的输出流)提供的 InputStream 来做同样的事情。

Here's how you get the output from the process.

以下是如何从过程中获取输出。

InputStream in = new BufferedInputStream( pr.getInputStream());

You can read from this until EOF, and write the output to a file. If you don't want this thread to block, read and write from another thread.

您可以从中读取直到 EOF,并将输出写入文件。如果您不希望此线程阻塞,请从另一个线程读取和写入。

InputStream in = new BufferedInputStream( pr.getInputStream());
OutputStream out = new BufferedOutputStream( new FileOutputStream( "output.txt" ));

int cnt;
byte[] buffer = new byte[1024];
while ( (cnt = in.read(buffer)) != -1) {
   out.write(buffer, 0, cnt );
}

回答by Kumar Vivek Mitra

1.After successfully executing the command from Java program, you need to read the output, and then divert the Output to the file.

1.从Java程序成功执行命令后,您需要读取输出,然后将输出转移到文件中。

Eg:

例如:

    Process p = Runtime.getRuntime().exec("Your_Command");

    InputStream i = p.getInputStream();

    InputStreamReader isr = new InputStreamReader(i);

    BufferedReader br = new BufferedReader(isr);


    File f = new File("d:\my.txt");

    FileWriter fw = new FileWriter(f);            // for appending use (f,true)

    BufferedWriter bw = new BufferedWriter(fw);

    while((br.readLine())!=null){


         bw.write(br.readLine());           // You can also use append.
  }

回答by davidbuzatto

Complementing Andy's answer, I think you MUSTread this article: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html.

补充安迪的回答,我认为你必须阅读这篇文章:http: //www.javaworld.com/jw-12-2000/jw-1229-traps.html

It is very important for who needs to deal with external processes in Java.

对于需要在 Java 中处理外部进程的人来说,这一点非常重要。

回答by AlexDev

I you want to keep it simple, and you are using Windows, try:

如果您想保持简单,并且您使用的是 Windows,请尝试:

Process pr = rt.exec("cmd /c \"ping localhost > output.txt\"");