• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PrintWriter after log()

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please look into the below code and tell me why PrintWriter object doesn't work when called after writing to log?

 
Niiraj Patel
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faisal,

For the output to be made correctly, PrintWriter object should be flushed and must be closed with its flush() and close() methods.

e.g.
rWriter.flush(); // Writes the remaining data to the screen
rWriter.close(); // Finishes the work

Regards,
Niraj
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Niraj!

Thanks so much! So, flushing and closing are must? Why it worked when rWriter was used prior to writing to log?
 
Niiraj Patel
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

flushing and closing are must?


PrintWriter object should be flushed and must be closed.

flush() should not be necessary but it's good to know that the PrintWriter does not flush before it closes the underlying stream.


Why it worked when rWriter was used prior to writing


PrintWriter has no relation with ServletContext.log()

Regards,
Niraj
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! Will remember it always.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic