How to find out if a file grows in Java (OS independent)

I am trying to find a cross-platform way of reliably describing whether the file ended with copying (external process).

Earlier on OSX / Linux I just checked

File file = new File("path/to/file"); file.length() 

then wait a few seconds and check it again to see if it has changed.

However, this does not work on Windows, since the method always returns the size of the full file (that is, in the end, it will be copied one day).

I also tried checking the lastModified timestamp, but that didn't change either.

+4
source share
2 answers

In the end, I solved this by calculating the checksum and hash of the file (rather than the length) at two different points in time and checking if they changed to determine if the file was still written.

0
source

One option is to try to open the file for recording using the "appending" FileOutputStream - if you do, it is unlikely that another file will also write the file. Of course, you do not need to write any data - just create a stream.

(Edited to remove an idea that is already in question, using lastModified .)

+2
source

Source: https://habr.com/ru/post/1437241/


All Articles