Home arrow static arrow Java Programming [Archive] - get File size
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - get File size
1 Duke Stars available
This topic has 3 replies on 1 page.

Posts:82
Registered: 3/10/04
get File size  
Jun 16, 2004 12:55 PM



 
hi people,
is there a way to get a file size without explicitly read all of its content? In general does the encoding of a file contain its size?
thanks a lot!
 

Posts:9
Registered: 6/16/04
Re: get File size  
Jun 16, 2004 1:02 PM (reply 1 of 3)



 
if you create a File object associated with a file you can use the .length() function to give you the size of the file in bytes. As far as I know it doesn't require reading the entire file.

AJ

 

Posts:82
Registered: 3/10/04
Re: get File size  
Jun 16, 2004 3:49 PM (reply 2 of 3)



 
thanks, if I have an URL pointing to a file, is there a way i can get this fie's length without reading its entire content? new File(new URI(url.toString())) will not work because the URI is not a file scheme.
 

Posts:334
Registered: 5/23/97
Re: get File size  
Jun 16, 2004 10:46 PM (reply 3 of 3)



 
http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLConnection.html#getContentLength()

However, this is only safe if you are sure your server returns a proper content header
because the relevant RFC said SHOULD and not MUST. Therefore it will
fail on some servers that ignore it. If you on;y use your own server and you know it
returns a content header then it should do the job.

(T)


 
This topic has 3 replies on 1 page.