How can I achieve the following, where prefsOutputStream is the pseudo-variable I'm aiming to make:
PrintStream oldOut = System.out;
System.setOut(prefsOutputStream);
System.out.println("Foo Bar");
String logString = Preferences.userRoot().node("app").get("stdout","");
oldOut.println(logString); // Outputs "Foo Bar" into the console
One way I thought of is:
This creates a
PrintStreamthat outputs to a byte array output stream. ThePrintStreamwill be automatically flushed whenever you print a newline character (e.g. by usingprintln). This will cause theByteArrayOutputStreamto flush, which will write everything written to the stream since the last flush, to the preferences.