Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: how to read back the lines printed out to the console? Date: Mon, 30 Jan 2012 21:18:21 -0800 Organization: albasani.net Lines: 55 Message-ID: References: <99599fa8-013c-4132-ac9e-5987d9d5588e@o13g2000vbf.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net TOfQ61kVBYwxklh2AnNWFwAX4bHNrAqacb2z62U+aDWfXWRYW72V1uQ42OLpk2VzM6bS6cRxqY3OB5SWBSxxOYFJuJy8hHwBjuZqzrxQbvS5spiZKlf5OZlMQgAI+bg1 NNTP-Posting-Date: Tue, 31 Jan 2012 05:18:17 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="6fdbCBnrLzbEeplT4YwnBBLiKvpcmpXN8hYoWXYQ2Uo5mdophp7tKIuVlKcEmXhANDbdBqFPOe4l8lk06D3cOENZwcixfQ3CZn79WuZuQj619SnhkIOfxlhIwYiTWTGU"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111229 Thunderbird/9.0 In-Reply-To: Cancel-Lock: sha1:puS3m5DVVeruSrwgzJ7e+608saA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11669 On 01/30/2012 06:49 PM, frame wrote: > Thank you very much for all the replies. Probably I didn't explain my > question clear. After reading all the replies, I am sorry I still > didn't get the idea to proceed. > > My problem is such: our finished Java program has many printing > statements, e.g. > > System.out.println("step A: everything is ok"); > ... > System.out.println("step B: computation starts now"); > ... > > if(so) > { > System.out.println("warning: temperature is too hot"); > } > > There are about 500 of those. When we run the program, all these > messages show up on the console -- that's good. But now, we also want > to keep those messages in a file. I have two ideas, but none of them > is acceptable: > > 1)at the beginning of the program, I added a little piece of code(re- > direct the System output to a file), now all the existing 500 > System.out.println(..) prints to a file, not the console any more -- > this is not good, because the program runs up to 5 hours, we hope to > see the message progressively on the console. With this approach, we > have to wait till the program finished and open the text file to read. > 2)another approach, use log4j to replace those 500 printing statements > -- we are not ready for this approach right now, because there are 500 > of them -- in many classes. log4j has different levels(INFO, WARNING, > ERROR etc). We need to analyze every printing case to decide its > level. It is a too big approach for now. > > Ideally, I was wondering if this is possible: at the end of the > program, when all those messages have been printed out to the console, > I can added a small piece of code, which will read all the lines on > the console back, then write them into a text file. I don't know if > this makes sense. People have already answered this question in detail, with code examples. But what you really need to do is take the suggestion to use a logging framework. Then you can change the configuration in a properties file with no code changes. 500 instances isn't very many, especially with a global 's/System.out.println/logger.info/'. Any decent IDE will let you do that in a single pop. Do that. Really. It's the right thing to do. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg