Here you can find the source of getFileSize(final String filePath)
public static String getFileSize(final String filePath)
//package com.java2s; import java.io.File; public class Main { public static String getFileSize(final String filePath) { String formattedFileSize = "0 B"; File fileReference = new File(filePath); if (fileReference.exists()) { long fileSizeInBytes = fileReference.length(); if (fileSizeInBytes < 1024) { return fileSizeInBytes + " B"; }/*from w w w. j ava 2 s . c om*/ int unitIndex = (int) (Math.log(fileSizeInBytes) / Math.log(1024)); String computedUnit = "KMGT".charAt(unitIndex - 1) + ""; formattedFileSize = String.format("%.1f %sB", fileSizeInBytes / Math.pow(1024, unitIndex), computedUnit); } return formattedFileSize; } }