Here you can find the source of formatSize(long bytes)
public static String formatSize(long bytes)
//package com.java2s; //License from project: Open Source License public class Main { public static String formatSize(long bytes) { long factor = 1; int pos = 0; String[] abrv = { "B", "KB", "MB", "GB" }; for (int i = 0; i < 4; i++) { if (bytes / factor == 0) { pos = i - 1;/*from w w w. ja v a 2 s. c om*/ break; } factor *= 1024; } return "" + ((bytes * 1024) / factor) + " " + abrv[pos]; } }