• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Moving output buffer to file

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try System.setOut( your file output stream ) ? Might want to setErr too.
 
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic