2

The javadoc for java.io.File.length() states:

Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.

I've been testing on Solaris which seems to be based on the number of files in the directory, but it always returns a greater number than there are files\folders including '.' files.

Does anyone know what this value means on Solaris\Linux\OSX\Windows? It's probably OS specific but it would be good to know if it has any value at all!

EDIT:

I agree with the comments which have advised that this is not a feature which should be used for the "Write Once, Run Anywhere" Java methodology, however since the value returned is not 0L (which it might be on some OS's but not Solaris) i'm interested in knowing what the value does refer to.

The native Java calls are usually direct single OS procedures so whatever the value refers to has some meaning and you get it for free without needing to load more java.io.File(s).

4

1 に答える 1

2

File.length は、主にファイルのサイズを決定するために使用されます。ディレクトリ内のファイル数を決定するために使用しないでください。未定義の JavaDocs 状態が警告ベルをトリガーしたはずであるという事実。

ディレクトリ内のファイル数がわからない場合は、代わりにFile.listFiles (またはその代替手段の 1 つ) を使用する必要があります。

于 2012-09-14T22:32:28.790 に答える