Java tutorial
//package com.java2s; import java.io.File; import java.text.DecimalFormat; public class Main { public static long getFileSize(String filePath) { long size = 0; File file = new File(filePath); if (file != null && file.exists()) { size = file.length(); } return size; } public static String getFileSize(long size) { if (size <= 0) { return "0"; } java.text.DecimalFormat df = new DecimalFormat("##.##"); float temp = (float) size / 1024; if (temp >= 1024) { return df.format(temp / 1024) + "M"; } else { return df.format(temp) + "K"; } } }