在 Java 中捕获标准输出的内容

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

Capturing contents of standard output in Java

javastdoutprintln

提问by Shailesh Tainwala

I am invoking a function that is printing some string in my console/standard output. I need to capture this string. I cannot modify the function that is doing the printing, nor change runtime behavior through inheritance. I am unable to find any pre-defined methods that will allow me to do this.

我正在调用一个在控制台/标准输出中打印一些字符串的函数。我需要捕获这个字符串。我无法修改正在执行打印的函数,也无法通过继承更改运行时行为。我无法找到任何允许我执行此操作的预定义方法。

Does the JVM store a buffer of printed contents?

JVM 是否存储打印内容的缓冲区?

Does anyone know of a Java method that will aid me?

有谁知道可以帮助我的 Java 方法?

采纳答案by Ingo

You could temporarily replace System.err or System.out with a stream that writes to string buffer.

您可以使用写入字符串缓冲区的流临时替换 System.err 或 System.out。

回答by Andreas Dolk

You can redirect the standard output by calling

您可以通过调用重定向标准输出

System.setOut(myPrintStream);

Or - if you need to log it at runtime, pipe the output to a file:

或者 - 如果您需要在运行时记录它,请将输出通过管道传输到一个文件:

java MyApplication > log.txt


Another trick - if you want to redirect and can't change the code: Implement a quick wrapper that calls your application and start that one:

另一个技巧 - 如果您想重定向并且无法更改代码:实现一个调用您的应用程序并启动该应用程序的快速包装器:

public class RedirectingStarter {
  public static void main(String[] args) {
    System.setOut(new PrintStream(new File("log.txt")));
    com.example.MyApplication.main(args);
  }
}

回答by Kartik Domadiya

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class RedirectIO
{

    public static void main(String[] args)
    {
        PrintStream orgStream   = null;
        PrintStream fileStream  = null;
        try
        {
            // Saving the orginal stream
            orgStream = System.out;
            fileStream = new PrintStream(new FileOutputStream("out.txt",true));
            // Redirecting console output to file
            System.setOut(fileStream);
            // Redirecting runtime exceptions to file
            System.setErr(fileStream);
            throw new Exception("Test Exception");

        }
        catch (FileNotFoundException fnfEx)
        {
            System.out.println("Error in IO Redirection");
            fnfEx.printStackTrace();
        }
        catch (Exception ex)
        {
            //Gets printed in the file
            System.out.println("Redirecting output & exceptions to file");
            ex.printStackTrace();
        }
        finally
        {
            //Restoring back to console
            System.setOut(orgStream);
            //Gets printed in the console
            System.out.println("Redirecting file output back to console");

        }

    }
}