java中的system.out.println重定向 [英] system.out.println redirection in java

查看:49
本文介绍了java中的system.out.println重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获println并重定向到文件并打印它

I want to catch the println and redirect to a file and print it

这是我的代码,我希望打印123/n 456

here is my code, I wish to print 123 /n 456

但这是行不通的,我认为在打印outContent之前我需要一些东西来阻止捕获,但是我不知道该怎么做.

but that doesn't work and I think I need something to stop the catching before I print the outContent but I don't know how to do it.

public static void main(String args[]){
    ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    System.out.println("123");
    System.out.println("456");
    System.out.println(outContent.toString());
}

推荐答案

回想过去,在我开始使用log4j或其他专用记录器之前,我曾经使用一种类似于下面的技术.

Way back in the day, before I started using log4j or other dedicated loggers, I use to use a technique similar to the one below.

本质上是代理 PrintStream ,它将内容回显到原始的 PrintStream ,并将内容写入其他 OutputStream (例如到文件).

Essentially it's a proxy PrintStream which echos content back to the original PrintStream as well as writing the content to some other OutputStream (such as to a file).

您还可以通过设置为true时不使用 old.write(b)来设置将回显到控制台的标志.我采用了这种技术来停止应用程序向stdout喷出大量废话,这可能会使应用程序变慢,尤其是在图形环境中运行时.

You could also put in flag that would turn of the echoing to the console by simply not using old.write(b) when set to true. This a technique I've employed to stop applications the spew copious amounts of crap to the stdout which can slow the application down, especially when you're running in a graphical environment...

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class RedirectStdOut {

    public static void main(String[] args) {

        OutputStream os = null;

        try {

            os = new FileOutputStream(new File("Log.txt"));
            LoggingPrintStream lps = new LoggingPrintStream(os);

            System.out.println("This is going to the console only...");
            lps.install();
            System.out.println("This is going to console and file");
            System.out.println("Fun times");
            lps.uinstall();
            System.out.println("This is going to console only...");

        } catch (IOException exp) {
            exp.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (Exception e) {
            }
        }

    }

    public static class LoggingPrintStream {

        private OutputStream os;
        private PrintStream old;

        public LoggingPrintStream(OutputStream os) {
            this.os = os;
        }

        public void install() {
            old = System.out;
            System.setOut(new PrintStream(new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    old.write(b);
                    os.write(b);
                }
            }));
        }

        public void uinstall() throws IOException {
            try {
                os.flush();
            } finally {
                try {
                    os.close();
                } catch (Exception e) {
                }
                System.setOut(old);
            }
        }

    }

}

我试图弄清楚是否可以串流,但是我的头不适合今天(星期五下午)的任务

I was trying to figure out if I could chain the streams, but my heads not up to the task today (Friday afternoon)

这篇关于java中的system.out.println重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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