File size

Hello all,

I would like to know what is the most efficient way in java to find out the size of a file. I need it to be efficient as I want to know the file sizes of about 400,00 files.

Currently I am using FileInputStream(file).available() but this operation seems too costly as when I do not perform this operation I can recurse every file and directory in about 10 second but as soon as I test for size it takes about somewhere in the ranges of 20 minutes!

Is some sort of meta data that is attached to a file to get its size or is this info platform dependent and would therefore have to use JNI.

Thanks for your help Martin.

[661 byte] By [MartyMarta] at [2007-11-27 6:15:30]
# 1

Unfortunately it's platform dependant. On *nix's filesystem the file size (for files upto 2 gig) is stored in the inode (the file which implements a "directory", so you can traverse the tree at warp speed... but NTFS ain't quit so (sorry bill) intelligent.

File.length() works on both platforms is about as quick as anything on windows, unless you're willing to get down into assembler and deal with raw partitions.

corlettka at 2007-7-12 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks, that helps a lot, I did not realise that File had its own method to return the size of a file, I simply assumed that length() returned some other vaule, doh! I will give File.length() a try and see if it is faster than FileInputStream.available().
MartyMarta at 2007-7-12 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 3

> Thanks, that helps a lot, I did not realise that File

> had its own method to return the size of a file, I

> simply assumed that length() returned some other

> vaule, doh! I will give File.length() a try and see

> if it is faster than FileInputStream.available().

It should be faster, and it should also return the correct size. FileInputStream.available might not return the correct length.

Kaj

kajbja at 2007-7-12 17:26:13 > top of Java-index,Java Essentials,Java Programming...