Here you can find the source of formatBytes(long bytes)
public static String formatBytes(long bytes)
//package com.java2s; //License from project: Open Source License public class Main { private static final int UNIT = 1024; private static final String B = "B"; private static final String KB = "KB"; private static final String MB = "MB"; private static final String GB = "GB"; private static final String TB = "TB"; public static String formatBytes(long bytes) { double bytesDouble = bytes; int unitIndex = 0; String unit;//from w w w . java2 s .co m while (bytesDouble >= UNIT) { unitIndex++; bytesDouble = Math.round(bytesDouble * 100 / UNIT) / 100.0; } switch (unitIndex) { case 0: unit = B; break; case 1: unit = KB; break; case 2: unit = MB; break; case 3: unit = GB; break; case 4: unit = TB; break; default: unit = "PB"; } return bytesDouble + " " + unit; } }