Here you can find the source of memorySize(long bytesSize)
public static String memorySize(long bytesSize)
//package com.java2s; //License from project: Apache License public class Main { public static String memorySize(long bytesSize) { if (bytesSize < 1024) { return stringBuffer(bytesSize, 'B'); } else if (bytesSize < 1024 * 1024) { float kb = (float) bytesSize / 1024.f; return stringBuffer(String.format("%.2f", kb), "KB"); } else {/*from w ww .ja va 2 s.co m*/ float mb = (float) bytesSize / 1024.f / 1024.f; return stringBuffer(String.format("%.2f", mb), "MB"); } } public static String stringBuffer(Object... strs) { StringBuilder buf = new StringBuilder(); for (Object str : strs) { buf.append(str); } return buf.toString(); } }