简体   繁体   中英

Handling write and read at the same time in java

I have a thread which will be continuously logging into a file. I have a function getLines() which when called will return last 100 lines of the log file.

My question is whether implementing a simple BufferedReader inside getLines() is enough ? I'm mainly concerned whether reading is valid when a write is going on. I don't mind missing a few lines of code which was written during the process of read though.

Thanks

Since Java FileOutputStream / FileInputStream open files in shared mode reading will not interfere with writing. Though in my view it would be better and more efficient to implement a logger that holds last 100 written lines and returns them on demand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM