Franz Kafka
Franz Kafka

Reputation: 10831

Is File.length platform and filesystem independant

Image I catch a stream of the net and count how many bytes went through my hands, I write this stream to a file. Is the number of counted bytes guranteed to be equal to the file.length() result (after flushing etc...)?

If you look at the Windows Explorer it gives you some different numbers (size on disk, real size,...).

Is the java file.length free of any OS and filesystem extensions to the file?

The javadoc only says: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist

Upvotes: 2

Views: 353

Answers (2)

Ravi Bhatt
Ravi Bhatt

Reputation: 3163

 number of counted bytes guranteed to be equal to the file.length()

I don't think so. Mostly no. It would depend on character encoding. Each character that you write in a file may take more than one byte of space.

Upvotes: -1

Denis Tulskiy
Denis Tulskiy

Reputation: 19177

Size on disk is how many bytes the file takes on the filesystem. It is not the file size. What file.length() returns is, as you quoted from the javadoc, how many bytes you can read from the file. So yes, it is platform independent.

Is the number of counted bytes guranteed to be equal to the file.length() result

if you're reading bytes and writing bytes, without converting to strings, etc. Then yes.

Upvotes: 2

Related Questions