-4

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).

skaffman
  • 398,947
  • 96
  • 818
  • 769
Spliff
  • 361
  • 1
  • 3
  • 6

3 Answers3

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
  • Yes, you'll need a `new File(...)` for that. – Philipp Reichart Nov 03 '11 at 08:50
  • @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