1

I need to determine the size of a very large character-encoded file. A read of the file takes a significant amount of time.

My understanding is that when a file is first created/modified the size is cached, so the OS can quickly retrieve the value when the size is requested, say, by a file manager. (eg. it seems quick when opening the properties dialog of a large file in win explorer)

Assuming the above is true, can this be retrieved in Java? I had thought that length() read the file to determine the size...or does it in fact get this cached size? Or does the creation of a File object do this read/retrieved the cached size?

My own research hasn't been able to answer these questions as yet.

I'd appreciate some help with my understanding

Thanks

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130

4 Answers4

5

getLength() delegates to the underlying native operating system function to get the length of the file. You should be fine using this.

spot35
  • 888
  • 4
  • 9
5

File systems generally store the length as a part of the file description. This way the OS knows where the end of the file is. This information is cached when accessed. And repeated calls for this information will also be cache.

Note: the OS often reads more data from disk than you ask for. This is because access to disk are expensive and memory is relatively cheap. e.g. when you get the length of one file it may read in the detail of many files on the assumption you might want information about those files too. i.e. the first time you get a file's information it is likely to already be cached.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

The length() method doesn't read the file. It calls a native method which delegates to the OS to get the file length. Its response time should not depend on the actual file length.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

I think you're over thinking this. Length should query the file system and figure this out very quickly. It's certainly not reading the entire file, and counting bytes.

jefflunt
  • 33,527
  • 7
  • 88
  • 126