Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Logging with PrintStream in Java

Posted on 2011-04-28
6
Medium Priority
?
571 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
0
Comment
Question by:bozer
  • 3
  • 3
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35482197
> 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
0
 

Author Comment

by:bozer
ID: 35483365
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 35487762
no close() *is* a real close.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
objects earned 200 total points
ID: 35488448
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
0
 

Author Closing Comment

by:bozer
ID: 35488821
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

810 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