开发者

How to limit the file size in Java

I am creating a file in my application and I am keep on writing some content in to this file.

But after my file reaches to some size, let say 100 lines, I want to erase the first line and write the new line to the bottom.

The requirement is my file should be limited, but it should maintain the latest content that I have开发者_运维百科 written in to file.

Please let me know if its possible in Java? Please let me know if there's any alternative to do this.


Please let me know if its possible in Java?

This can't be done in a simple way. You can only truncate the last part of a file. (This is an inherit limit from the underlying file system, and due to the same reasons as to why you can't insert bytes in the middle of a file.)

You can do it manually by:

  1. Figuring out the byte-length, n, of the first line.
  2. For i = n ... length of file
    1. Write byte at pos i to pos i - n
  3. Truncate the file to length length of file - n.

Please let me know if there's any alternative to do this.

I suggest you solve it using a cyclic buffer or something similar. Still though, you'd get into trouble if not all lines are equally long, unless you keep the buffer in memory, and dump it to (a cleared) file.


This is do-able but could be a very slow process depending on how you implement it. At least keep the 100 lines in memory in a data structure like a Vector so that you don't have to read the file each time, just write it each time you drop the first element and append the new final element.

Another alternative would be to store each line in its own file. Then you just delete a file and make a new one, for each new line after the 100th.

[much later] Thought of another way, fixed-length records, if your lines are of predictable size. Each line is a space followed by the text followed by spaces to pad out the record, with end-of-line character(s) at the end if desired. Each time you write a record, you prepend it with an asterisk to mark it as the current line, and then index into the previous line (with the modulo operator so it automatically wraps around) and replace that asterisk with a space. An example 10-line logfile would look something like this:

 this happened at that time                        
 blah blah happened                             
*this is the current record
 this was the fourth event to occur after startup
 this was the fifth event to occur after startup
 this was the sixth event to occur after startup
 this was the seventh event to occur after startup
 this was the eighth event to occur after startup
 this was the ninth event to occur after startup
 this was the tenth event to occur after startup

This way you only need to open the file, index to the current line (which you know because you're keeping track in your program; otherwise, on restart, loop to find the asterisk), and overwrite it, padding it with spaces to the chosen length. then index back one record and replace the asterisk with a space.


I'd use a cyclic buffer. The way I'd implement it with 2 files, and start using the second file if the first one contains 100 lines. Switch back to writing to the first, when that file gets to 100 lines. That way you'll always have 100 to 200 lines total (once you get to at least 100 lines the first time).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜