user3655225 user3655225 - 1 month ago 14
Java Question

Timer based file persistent connection in java

I have an application in which frequent write operation is done in a day-wise file.I dont want to open-close file for every write.

A timer based application will start at 12:01 am everyday and creates a Thread. In this thread, a file open connection will be created. So when a GET request comes for write operation, it will simply check if thread is running and write into the file. At next 12:01 am, previous thread will close file connection.

Any suggestion to how to achieve this??
I am new to Java. Pls help


You don't need another thread.

Just use a Singleton to perform the writing. Each time you need to write, invoke a method on your writer object. That writer object checks the current date, and looks to see if the currently opened file is appropriate to the current date. If so, write. If not, close the file, create/open the appropriate file for today, and then write.

An object with a member variable that points to an open file will keep that file open until that object goes out of scope and is garbage collected.

The point is that you really don't care about the rolling over at the moment of midnight. You only care at the moment you need to write.