/*
Get File size in bytes
This Java example shows how to get the size of a particular file in bytes,
KB and MB using length method of Java File class.
*/
import java.io.*;
public class GetFileSizeInByetes {
public static void main(String[] args) {
//create file object
File file = new File("C://FileIO/demo.txt");
/*
* To get the size of a file, use
* long length() method of Java File class.
*
* This method returns size of a particular file in bytes. It returns 0L
* if file does not exists, and unspecified if file is a directory.
*
*/
long fileSize = file.length();
System.out.println("File size in bytes is: " + fileSize);
System.out.println("File size in KB is : " + (double)fileSize/1024);
System.out.println("File size in MB is :" + (double)fileSize/(1024*1024));
}
}
/*
Output would be
File size in bytes is: 492480
File size in KB is : 480.9375
File size in MB is :0.46966552734375
*/
Bookmark/Search this post with:
Post new comment