Logging with PrintStream in Java

Posted on 2011-04-28
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);

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?

Question by:bozer
    LVL 92

    Expert Comment

    > 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 Comment

    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?
    LVL 92

    Expert Comment

    no close() *is* a real close.

    Author Comment

    Can you please give more information? It's not an actual 'open' and append of a text file but 'close' closes the file?
    LVL 92

    Accepted Solution

    The file actually gets opened prior to the code you posted, so cannot tell if it is opened to append or not

    >            ps.close();

    that line closes the file

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now