File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Limit log file size, with 'scrolling'. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Limit log file size, with Watch "Limit log file size, with New topic

Limit log file size, with 'scrolling'.

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am creating a log file which grows very quickly to a huge size. I would like to fix the size of the file to say 1MB and if there is new data, 'scroll' the old data at the top of the file off and append new data at the end. (Circular file).
Any ideas on how to implement this?
Right now i am using a FileOutputStream with append TRUE to append to the log file.
Any help would be appreciated!
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Well, once a line is written to a file, the only way to remove it is to completely rewrite that file or over write that data. Another concept you can use is to implement your file as a random access file. Once you reach the max number of items in your file, you start writing them again at the start of the file. This presents a problem when you try and read or display the contents but, I think you can find a way to write a log display program that can get around the limitation of this approach.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Carl,
Yeah, i was thinking of going the Random Access File route. Rewriting would have to be done at the top of the file and not 'scrolled' as i had first thought so the display would definitely need some work.
Unless i tried to 'scroll' backwards so the stales log would be at the bottom of the file and the top of the file would have the latest log data. This would mean manipulating the File Pointer somehow and restricting the file size....
..anyway, thanks for your input!
I agree. Here's the link:
subject: Limit log file size, with 'scrolling'.
It's not a secret anymore!