We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Logging with PrintStream in Java

bozer
bozer asked
on
Medium Priority
639 Views
Last Modified: 2012-05-11
Hello experts,

We are trying to understand how PrintStream works when used for logging. There are several examples over the internet but we need to understand how that writes data in a text file. Here is the example:

            ps = new PrintStream(fos);
                ps.print(midLog+"/"+data1+"\r\n");
            ps.close();

Let's say we have that piece of logging in a web application and that application is used frequently, how will it work? Does it open and close the text file every time the example code is repeated?

Thanks
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Does it open and close the text file every time the example code is repeated?

that code appears to
to avoid that keep  a reference to the PrintStream and don't close it

though why not use a logging framework

Author

Commented:
Thank you,

So 'close' method is not a real 'close' in that sense. So how do we destroy that object? (Printstream) And if that does not gets destroyed, what are the consequences?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
no close() *is* a real close.

Author

Commented:
Can you please give more information? It's not an actual 'open' and append of a text file but 'close' closes the file?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.