size (i.e. length) of an OS file: gotten from the OS ?

  • Thread starter =?iso-8859-1?q?S=E9bastien_de_Mapias?=
  • Start date
?

=?iso-8859-1?q?S=E9bastien_de_Mapias?=

Hi,
I'd like to know how Java knows the size of a file.
When you simply do
File f = new File("/usr/tmp/stuff.txt");
long si = f.length();

it performs a call to its own reference of FileSystem.getLength
on itself, and when I check the getFileSystem() method in the
FileSystem class I see it's native...

So I guess the size (amongst other properties) is a data that
f receives when it's instantiated - or I'm mistaken ?

Thanks.
Seb
 
D

Daniel Pitts

Sébastien de Mapias said:
Hi,
I'd like to know how Java knows the size of a file.
When you simply do
File f = new File("/usr/tmp/stuff.txt");
long si = f.length();

it performs a call to its own reference of FileSystem.getLength
on itself, and when I check the getFileSystem() method in the
FileSystem class I see it's native...

So I guess the size (amongst other properties) is a data that
f receives when it's instantiated - or I'm mistaken ?

Thanks.
Seb
It probably isn't when its instantiated, but rather when that method is
invoked... A File object doesn't actually represent a file, but a
file-location. Therefor, the file itself can change over time.

Being that it is a native call, I'm guessing that it is translated to a
call to the OS.
 
R

Roedy Green

So I guess the size (amongst other properties) is a data that
f receives when it's instantiated - or I'm mistaken ?

nope. The implementation is:
return fs.getLength(this);


where fs is the file system. It does not call getLength until you
call File.length()

the length can change over time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top