Here you can find the source of bytesToString(long size)
public static String bytesToString(long size)
//package com.java2s; //License from project: Apache License public class Main { /**//from w ww. j a va 2s . co m * Convert a quantity in bytes to a human-readable string such as "4.0 MB". */ public static String bytesToString(long size) { long TB = 1L << 40; long GB = 1L << 30; long MB = 1L << 20; long KB = 1L << 10; double value; String unit; if (size >= 2 * TB) { value = (double) size / TB; unit = "TB"; } else if (size >= 2 * GB) { value = (double) size / GB; unit = "GB"; } else if (size >= 2 * MB) { value = (double) size / MB; unit = "MB"; } else if (size >= 2 * KB) { value = (double) size / KB; unit = "KB"; } else { value = (double) size; unit = "B"; } return String.format("%.1f %s", value, unit); } }