Search code examples
javaunixapache-commons

Touch a file using apache FileUtils


I have looked at the source code of Apache Commons FileUtils.java class to see how they implement unix like touch functionality. But I wanted to confirm with the community here if my use case would be met by the implementation as it opens and closes a FileOutputStream to provide touch functionality

We have two webservers and one common server between them where a File is residing

For our application we need to use the time modified of this file to make some decisions. We actually don't want to modify the file but change its last modified date when some particular activity happens on one of the webservers.

Its important that last modified time set for the file is taken from the central server to avoid worrying about time differences between two web servers. Therefore changing file.setLastModfiied is not a good option as webserver would send its own time.

But I am wondering that even if I use Apache Commons FileUtils touch method to do this, would closing stream on one webserver set the last modified time of the file using time of the webserver or the central server.

Sorry for so much details but could not see any other way to explain the issue


Solution

  • If you "touch" a file in the filesystem of one webserver, then the timestamp of the file will be set using the clock of that server. I don't think you can solve your problem that way.

    I think you've got three options:

    • configure the servers to synchronize their clocks to the common timebase; e.g. using NTP,
    • put all files whose timestamps must be accurate to the common timebase on one server, or
    • change your system design so that it is immune to problems with different servers' clocks being out of sync.