0

In Java is there any way to check the size of a file without reading it into memory? The use case is just validation before any malicious activity, before handing over to another thread to do the parsing.

Thank you.

Thar
  • 2,956
  • 2
  • 10
  • 9
  • 1
    This question seems to be not about HOW TO KNOW, but HOW TO CHECK. I don't see a reason to close and point to different question. Actually, this isn't an easy question to answer. File.length() and real availability of bytes in the file can be not synchronized. If you know expected length of file (you have some info in the beginning of the file, for example), you can open the file in random access mode and try to real last bytes. If no IOExceptions, the file writing is finished. Or you know what bytes the file must ends with. Then you read the last bytes and check them. But in your case... – AnatolyG Nov 16 '20 at 12:35
  • you have 2 threads in one single process. Just let 2st thread to know when 1st one has its job done and file is flushed, closed and ready to be read (via a blocking queue, for example) – AnatolyG Nov 16 '20 at 12:35
  • Also, 1st thread may write a file with a .tmp name, and after the writing is done, the thread renames the file to a normal form, which is visible for the 2nd thread... – AnatolyG Nov 16 '20 at 12:37

0 Answers0