This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "logging" Watch "logging" New topic
Author

logging

shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Hi,
I have a process running for a long time during which i need to log some information about the process to some file.
I am not supposed to use new BufferedWriter(new FileWriter(filename)) because i should not keep the file opened during the process.
I need a way to store log info in some stream/sink and write it to the file only at the end of the process. This way I can avoid having the file opened during the process.
I tried using StringWriter to store the log and finally writing it to the file as follows:
FileWriter file = new FileWriter(filename);
file.write(mystringwriter.toString());
where mystringwriter is the StringWriter.
This gives OutOfMemory error becos the StringBuffer it uses internally exceeds its capacity.
Could anyone suggest any ideas on this.
Regards,
shankar
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
Can you occasionally dump the String to the file say when it reaches a certain size?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Is the program really running out of heap, or is it hitting some other limit on the size of the buffer of a StringBuffer? If the latter, I am interested to hear, because I did not know of any such limit.
If there is a limit on the size of the buffer in a StringBuffer, but your data will fit in memory, then you could change the structure in which you store it. Perhaps, an ArrayList of Strings (one per item of log) would help you.
If you really are running out of heap, and cannot just allocate more heap (via the -Xmx switch on the java command, for instance), then it is more difficult to offer a solution.
Compression, via a DeflaterOutputStream, for instance, might gain you something, as logging text is likely to be highly compressible.
Otherwise, you are going to have to write your data to file somewhere, because it just plain won't fit in memory.
You could, as an earlier respondent has suggested, write to your log file in short bursts, if it is acceptable for you to open the log file at all.
If you cannot open the log file at all, you could write to a different temporary log file, then copy its contents to the real log file, later. Copying is quickest via RandomAccessFile, rather than any type of stream, by the way.
There are pre-written log packages available. One, I think, is Log4J.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: logging
 
Similar Threads
jPedal PDF page to JPG
Do we need to close FileWriter Object???
how to empty a file
File Reading
Opening files in append mode