Is there any way to determine a file's size without having to open it, as provided in the Java platform API (no 3rd party libs)? Also, I need the 64-bit size (which I assume Java would provide to begin with).
Asked
Active
Viewed 1,622 times
-4
-
http://stackoverflow.com/questions/116574/java-get-file-size-efficiently – Anish Dasappan Nov 03 '11 at 08:57
-
1Zero research effort. Downvote. – user207421 Nov 03 '11 at 09:47
3 Answers
3
Yes, calling .length()
on a File
Object will return its size in bytes.

Bohemian
- 412,405
- 93
- 575
- 722

Sahil Muthoo
- 12,033
- 2
- 29
- 38
-
That would require constructing an object first opening the file, though; or at least that's what I thought, correct? – Spliff Nov 03 '11 at 08:49
-
@Bohemian, Grammar Nazi! Just kidding, I was about to fix it myself. – Sahil Muthoo Nov 03 '11 at 08:49
-
-
@Spliff A File object is an abstract pathname. Effectively it is a utility class that wraps a String. No file opening is required. – Nov 03 '11 at 08:51
-
@Spliff, There is no significant overhead in constructing a `File` Object. Java won't start reading the file contents when you call `new file()`. `.length()` returns the size reported by the file system. – Sahil Muthoo Nov 03 '11 at 08:52
0
long File.length()
. A File
object usually represents a path, rather than an open file.

Vlad
- 18,195
- 4
- 41
- 71
0
Without constructing any object for a particular file,How can you use the properties of file class. OPEN a file means that you are decoding the file data in your compatible format(.txt/.bmp/.pdf etc). SO you have to first
File file=new File("File Name");
File Name-->file name of file to which you want to deal. Then use file.length();
In this phenomenon you are not opening any file,you are just passing reference(address) of the file and retrieving its size. thats it. i hope this will beneficial to you.

SilentBomb
- 168
- 2
- 11