Here you can find the source of getFileSize(String filename)
public static String getFileSize(String filename)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.text.DecimalFormat; public class Main { public static String getFileSize(String filename) { File file = new File(filename); return getReadableFileSize(file.length()); }//from ww w.j a v a2 s . c o m public static String getReadableFileSize(long size) { if (size <= 0) return "0"; final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; } }