Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3487790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:11:32+00:00 2026-05-18T11:11:32+00:00

I have a Java program that outputs some text into console. It uses print

  • 0

I have a Java program that outputs some text into console. It uses print, println, and some other methods to do this.

At the end of the program , I want to read all the text in console and copy it into a String buffer. How could I do this in Java ? I need to read stdout and stderr separately.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:11:32+00:00Added an answer on May 18, 2026 at 11:11 am

    Ok, this was a fun problem. Dosen’t seem to be an elegant way of solving it for all PrintStream methods at once. (Unfortunately there is no FilterPrintStream.)

    I did write up an ugly reflection-based workaround though (not to be used in production code I suppose 🙂

    class LoggedPrintStream extends PrintStream {
    
        final StringBuilder buf;
        final PrintStream underlying;
    
        LoggedPrintStream(StringBuilder sb, OutputStream os, PrintStream ul) {
            super(os);
            this.buf = sb;
            this.underlying = ul;
        }
    
        public static LoggedPrintStream create(PrintStream toLog) {
            try {
                final StringBuilder sb = new StringBuilder();
                Field f = FilterOutputStream.class.getDeclaredField("out");
                f.setAccessible(true);
                OutputStream psout = (OutputStream) f.get(toLog);
                return new LoggedPrintStream(sb, new FilterOutputStream(psout) {
                    public void write(int b) throws IOException {
                        super.write(b);
                        sb.append((char) b);
                    }
                }, toLog);
            } catch (NoSuchFieldException shouldNotHappen) {
            } catch (IllegalArgumentException shouldNotHappen) {
            } catch (IllegalAccessException shouldNotHappen) {
            }
            return null;
        }
    }
    

    …that can be used like this:

    public class Test {
        public static void main(String[] args) {
    
            // Create logged PrintStreams
            LoggedPrintStream lpsOut = LoggedPrintStream.create(System.out);
            LoggedPrintStream lpsErr = LoggedPrintStream.create(System.err);
    
            // Set them to stdout / stderr
            System.setOut(lpsOut);
            System.setErr(lpsErr);
    
            // Print some stuff
            System.out.print("hello ");
            System.out.println(5);
            System.out.flush();
    
            System.err.println("Some error");
            System.err.flush();
    
            // Restore System.out / System.err
            System.setOut(lpsOut.underlying);
            System.setErr(lpsErr.underlying);
    
            // Print the logged output
            System.out.println("----- Log for System.out: -----\n" + lpsOut.buf);
            System.out.println("----- Log for System.err: -----\n" + lpsErr.buf);
        }
    }
    

    Resulting output:

    hello 5
    Some error
    ----- Log for System.out: -----
    hello 5
    
    ----- Log for System.err: -----
    Some error
    

    (Note though, that the out field in FilterOutputStream is protected and documented, so it is part of the API 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java client program that uses mdns with service discovery to find
Suppose that I have a Java program within an IDE (Eclipse in this case).
I have a Java program that loads thirdparty class files (classes I did not
We have a java program that requires a large amount of heap space -
Let's say I have a java program that makes an HTTP request on a
I have a single user java program that I would like to have store
We have a Java program run as root on Unix, that therefore can read
I have written a program in Java to read in a text file of
I'm writing a Java program that searches for and outputs cycles in a graph.
I currently have a small Java program which I would like to run both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.