File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Moving output buffer to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Moving output buffer to file" Watch "Moving output buffer to file" New topic
Author

Moving output buffer to file

Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Hai friends,
I have an application where i have given a lot of system.out.println statements. I want to save all these printing contents to be moved to a file. In C prog i have done this by giving the file pointer as 0 and whatever i prints on the screen moves to the file. Is there any facility available here with java.


Jithesh K
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Jithesh
Well can't you just redirect via system's redirection of output (if you are running your program from a prompt)? I mean like,
bash$java program > output.txt
OR you can use Streams' PrintWriter following way,
PrintWriter out = new PrintWriter(new FileWriter(new File("output.txt")));
and then use 'out' for printing wherever you would put System.out.println() (you have to just use out.println() method here)....
Regards
Maulin
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Thanks Moulin,
I was really trying hard to do that. because i did that in c and not able to do it over in java. In this case what actually happens. whether the file pointer is set to zero or just resetting the out class.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Try System.setOut( your file output stream ) ? Might want to setErr too.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
Did you think about using aspects and point cuts? I have a feeling that you can use AOP in cases like this. Try posting this in Java Other products and servers thread. Author Ramnivas Laddad is presently answering questions.

Thanks,
Vasu


What a wonderful world!
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Hai friends,
Could u just explain in brief that what is the AOP.

My problem is just to move th contents to written on the screen to file. It should be done by just giving the system.out statement.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Aspect Oriented Programming is a pretty cool new thing. Crosscutting is a key word. You define some change to make to Java code in a file outside your normal Java source. You tell it how to identify the spot - maybe on entry to every method, or when a particular exception is thrown - and what to change - say log the method entry or exception. The AOP processor applies these changes to your source code and/or compiled byte code (not sure). The change "cuts across" any number of methods or classes. I'm not sure if I like the idea or not yet.
AspectJ Guide
AspectJ Home
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Most of the logging APIs out there support redirection of System.out to a log file. I know log4J does for sure. You wouldn't have to change all of your printlns if you went with that solution.
brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Moving output buffer to file