This section covers a source code that will give the size of a file in bytes. The length method of File class gives the size of the file in bytes however if the abstract filename is a directory the return value is unspecified.

package com.javatutorialhq.tutorial;

import java.io.File;

/**
 * This sample code shows
 * how to check the file size
 * Property of javatutorialhq.com
 * All Rights Reserved
 * Version 1.0
 * 08/01/2012
 */

public class FileSizeCheck {

	public static void main(String[] args) {
		File f = new File("D:\\Websites\\index.html");
		//check first if file exist
		if(f.exists()){
			System.out.println(f.length() + " bytes");
		}
		else{
			System.out.println("File "+ f.getAbsolutePath() + " does not exists");
		}

	}

}

Sample Output:

5339 bytes