8

I need the filesize on disk and not the file size.

File myFile = new File ("C:\\Send\\Capture.png");
System.out.println((int)myFile.length());

This code gives the file size. I need the file size on disk. They always happen to be different, like in the picture below, right?

enter image description here

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
GrowinMan
  • 4,891
  • 12
  • 41
  • 58
  • In Java there is nothing to do with hadware or Operating System. You can only find the No. of bytes in a File not in terms of disk space.But using JNI from Java you may find. – Subhrajyoti Majumder Apr 21 '12 at 14:09
  • @SubhrajyotiMajumder no, there is no any philosophic problem to have such thing in a java api – Yura Oct 22 '22 at 13:23

1 Answers1

1

Have a look at link

the answer is provided for c# , but since it uses win32 API functions , you can easily convert it to java. the function used to get the size on disk is GetCompressedFileSize

hope that helps

Community
  • 1
  • 1
CyprUS
  • 4,159
  • 9
  • 48
  • 93