Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11641
| Date | 2012-01-30 19:33 -0500 |
|---|---|
| From | Arne Vajhøj <arne@vajhoej.dk> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: how to read back the lines printed out to the console? |
| References | <99599fa8-013c-4132-ac9e-5987d9d5588e@o13g2000vbf.googlegroups.com> |
| Message-ID | <4f2736dd$0$291$14726298@news.sunsite.dk> (permalink) |
| Organization | SunSITE.dk - Supporting Open source |
On 1/30/2012 3:03 PM, frame wrote:
> We have an existing Java program, which prints out a lot of message
> using System.out.println() method to the console. There are about 500
> those calls in the code. We hope to add one more feature: besides
> print out to the console as it is doing now, we also want to store
> those messages in a text file. So the message will be in two places:
> one place -- the console -- is shown the message progressively as the
> program is running; another place -- a text file -- is created at the
> end of the program.
>
> Since there are about 500 calls in the code, we don't want to add a
> duplicated printing method at every printing place. I am thinking to
> let the program run as usal, printing out all the messages to the
> console, then before the program ends, having a method reading in
> every line on the console, which was printed out previously. I just
> don't know how to achieve that.
If you can live with the file being written while the
program runs then something like:
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
public class TeeOutputStream extends OutputStream {
private OutputStream os1;
private OutputStream os2;
public TeeOutputStream(OutputStream os1, OutputStream os2) {
this.os1 = os1;
this.os2 = os2;
}
public void write(byte[] b) throws IOException {
os1.write(b);
os2.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
os1.write(b, off, len);
os2.write(b, off, len);
}
public void write(int b) throws IOException {
os1.write(b);
os2.write(b);
}
public void flush() throws IOException {
os1.flush();
os2.flush();
}
public void close() throws IOException {
os1.close();
os2.close();
}
public static void test2() {
System.out.println("This is test2");
}
public static void test1() {
System.out.println("This is test1");
}
public static void main(String[] args) throws IOException {
System.out.println("normal");
PrintStream sav = System.out;
System.setOut(new PrintStream(new TeeOutputStream(System.out, new
PrintStream("C:\\work\\tst.out"))));
test1();
test2();
System.setOut(sav);
System.out.println("normal");
}
}
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to read back the lines printed out to the console? frame <xsli2@yahoo.com> - 2012-01-30 12:03 -0800
Re: how to read back the lines printed out to the console? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-01-30 20:18 +0000
Re: how to read back the lines printed out to the console? Roedy Green <see_website@mindprod.com.invalid> - 2012-01-30 20:42 -0800
Re: how to read back the lines printed out to the console? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-30 14:18 -0800
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-30 19:34 -0500
Re: how to read back the lines printed out to the console? Patricia Shanahan <pats@acm.org> - 2012-01-30 14:37 -0800
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-30 20:08 -0500
Re: how to read back the lines printed out to the console? Patricia Shanahan <pats@acm.org> - 2012-01-30 17:18 -0800
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-30 21:00 -0500
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-30 19:33 -0500
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-30 19:35 -0500
Re: how to read back the lines printed out to the console? frame <xsli2@yahoo.com> - 2012-01-31 10:09 -0800
Re: how to read back the lines printed out to the console? Lew <noone@lewscanon.com> - 2012-01-31 11:11 -0800
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-31 20:39 -0500
Re: how to read back the lines printed out to the console? frame <xsli2@yahoo.com> - 2012-02-01 06:01 -0800
Re: how to read back the lines printed out to the console? Ian Shef <invalid@avoiding.spam> - 2012-02-01 19:15 +0000
Re: how to read back the lines printed out to the console? frame <xsli2@yahoo.com> - 2012-02-01 12:59 -0800
Re: how to read back the lines printed out to the console? Gene Wirchenko <genew@ocis.net> - 2012-02-01 14:00 -0800
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-02-01 19:27 -0500
Re: how to read back the lines printed out to the console? frame <xsli2@yahoo.com> - 2012-01-30 18:49 -0800
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-30 21:56 -0500
Re: how to read back the lines printed out to the console? Patricia Shanahan <pats@acm.org> - 2012-01-30 19:06 -0800
Re: how to read back the lines printed out to the console? Roedy Green <see_website@mindprod.com.invalid> - 2012-01-30 20:46 -0800
Re: how to read back the lines printed out to the console? Lew <noone@lewscanon.com> - 2012-01-30 21:18 -0800
Re: how to read back the lines printed out to the console? Arne Vajhøj <arne@vajhoej.dk> - 2012-01-31 20:36 -0500
Re: how to read back the lines printed out to the console? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-01-31 10:18 +0200
Re: how to read back the lines printed out to the console? Lew <noone@lewscanon.com> - 2012-01-31 11:15 -0800
Re: how to read back the lines printed out to the console? bugbear <bugbear@trim_papermule.co.uk_trim> - 2012-01-31 09:50 +0000
Re: how to read back the lines printed out to the console? Lew <noone@lewscanon.com> - 2012-01-31 11:17 -0800
csiph-web