0

I have a log file in my app that is constantly being written to and sometimes i need to check its size. The problem is that whenever i check its size quickly in another thread it sometimes returns 0, even though there is clearly data in it.

For example the file size output would look something like this: (numbers are bytes)

E/DataLogger: Size: 3403
E/DataLogger: Size: 3454
E/DataLogger: Size: 3454
E/DataLogger: Size: 3454
E/DataLogger: Size: 0
E/DataLogger: Size: 3500
E/DataLogger: Size: 3500
E/DataLogger: Size: 3812

I also tried with FileUtils.readFileToByteArray() and noticed the same behavior. Why do both these methods randomly return 0 sometimes?

TychoTheTaco
  • 664
  • 1
  • 7
  • 28

2 Answers2

1

It turns out I was occasionally overwriting the file, which briefly set the file size to 0 before new data could be written.

TychoTheTaco
  • 664
  • 1
  • 7
  • 28
0

I don' t know the logging API you are using but most of logging APIs have a rotation system when the file reachs a defined size. When the rotation happens, the logger may do some move operations on the file or on the file content for archiving the actual log file. Which explains the file size to 0 at a time.

In general, you may customize the file rotation (file size triggering the archiving rotation, number of archived logs, etc...)

To know further, you can study the rotation params of your logging API.

davidxxx
  • 125,838
  • 23
  • 214
  • 215