user2901156
user2901156

Reputation: 67

system.out.println redirection in java

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

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

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());
}

Upvotes: 0

Views: 493

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347244

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.

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).

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)

Upvotes: 1

Chamil
Chamil

Reputation: 803

Redirect System.out.println to Log4J, while keeping class name information

the above post explains how to do this to some extent.

Upvotes: 0

Sanjeev
Sanjeev

Reputation: 9946

You should redirect the out stream to a file like this:

 System.setOut(new PrintStream(new File("<your log file>"));

this will redirect all your system.out in the log file. hope this helps.

Upvotes: 0

Related Questions