Here you can find the source of formatFileSize(long size)
public static String formatFileSize(long size)
//package com.java2s; //License from project: Apache License import java.text.DecimalFormat; public class Main { private static final String[] SIZE_UNIT = { " B", " kiB", " MiB", " GiB", " TiB" }; private static final DecimalFormat FSIZE = new DecimalFormat("0.00"); /** create a formatted filesize string */ public static String formatFileSize(long size) { float tmpSize = size; int idx = 0; while (tmpSize > 1024) { tmpSize /= 1024.0f;//from ww w. j a va2 s .c om ++idx; } return FSIZE.format(tmpSize) + SIZE_UNIT[idx]; } }