Write a text file with a defined maximum lines count in java
Hi all Is it p开发者_StackOverflowossible to write a .txt file in java with a certain number of lines?
Pratically I have to write a file in append mode, for example with a maximum of 4 lines:
File .txt 1
item1
item2
item3
item4
When the files reaches 4 lines, writing on it can't be allowed and a new file is generated
File .txt 2
item5
item6
....
Thanks
Most logging frameworks do this. Why reinvent the wheel? Look into SLF4J.
I don't know if there is a library API for this, but it wouldn't be too hard to write your own. Upon opening the file for writing, you could read the number of lines and then count them as they are written, rolling over as necessary
YOu could use RandomAccessFile to do both reading and writing in one pass http://download.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html
精彩评论