Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Moving output buffer to file

 
Jithesh Kozhipurath
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try System.setOut( your file output stream ) ? Might want to setErr too.
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jithesh Kozhipurath
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic