public static String readableFileSize(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];
}
This will work up to 1000 TB.... and the program is short!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…