How to get file size in Java

In Java, you can use the File.length() method to get the file size in bytes.

Example

Get an image file (c:\\java_xml_logo.jpg) 14KB, and display the file size.


package com.mkyong.file;

import java.io.File;

public class FileSizeExample 
{
    public static void main(String[] args)
    {	
		File file =new File("c:\\java_xml_logo.jpg");
		
		if(file.exists()){
			
			double bytes = file.length();
			double kilobytes = (bytes / 1024);
			double megabytes = (kilobytes / 1024);
			double gigabytes = (megabytes / 1024);
			double terabytes = (gigabytes / 1024);
			double petabytes = (terabytes / 1024);
			double exabytes = (petabytes / 1024);
			double zettabytes = (exabytes / 1024);
			double yottabytes = (zettabytes / 1024);
			
			System.out.println("bytes : " + bytes);
			System.out.println("kilobytes : " + kilobytes);
			System.out.println("megabytes : " + megabytes);
			System.out.println("gigabytes : " + gigabytes);
			System.out.println("terabytes : " + terabytes);
			System.out.println("petabytes : " + petabytes);
			System.out.println("exabytes : " + exabytes);
			System.out.println("zettabytes : " + zettabytes);
			System.out.println("yottabytes : " + yottabytes);
		}else{
			 System.out.println("File does not exists!");
		}
    		
    }
}

Result


bytes : 13900.0
kilobytes : 13.57421875
megabytes : 0.013256072998046875
gigabytes : 1.2945383787155151E-5
terabytes : 1.2641976354643703E-8
petabytes : 1.234568003383174E-11
exabytes : 1.205632815803881E-14
zettabytes : 1.1773757966834775E-17
yottabytes : 1.1497810514487085E-20

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
11 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
11 Comment authors
KárililyRajeevanKannanumberto Recent comment authors
newest oldest most voted
umberto
Guest
umberto

i love you mkyong i think about you at night ;)

Adam Smith
Guest
Adam Smith

I have a yottabyte of porn on my laptop

Rajeevan
Guest
Rajeevan

Wil it loads the file to the memory? Does calling file.length() iterate through each byte of the file? I mean, will it take the same time to call file.length() & print the chars in the file?

Reyner
Guest
Reyner

Really nice and helpful sir, thank you once again

lily
Guest
lily

Then where i can make changes in this code to make it measure the entered text by user ?

nishant
Guest
nishant

Very helpful sir…keep up the good work

Steve
Guest
Steve

Very helpful… Thanks for creating this!

@Kjellski
Guest
@Kjellski

Hey mkyong,

I just wanted to tell you that I keep getting to your posts and I love them.
Always deliver, simple clean and reproducable results. Keep the work up, even
the simple tricks sometimes help.

Thanks,
@kjellski

Scott Duncan
Guest
Scott Duncan

Love your posts. Always helpful.

trackback
Java I/O Tutorials | J2EE Web Development Tutorials

[…] Get file size […]

Kannan
Guest
Kannan

Thank u very much sir,your post all really amazing stuff for learners,