Here you can find the source of toReadableSize(long bytes)
public static String toReadableSize(long bytes)
//package com.java2s; // Licensed to the Apache Software Foundation (ASF) under one import java.util.Formatter; import java.util.Locale; public class Main { protected static final long KB = 1024; protected static final long MB = 1024 * KB; protected static final long GB = 1024 * MB; protected static final long TB = 1024 * GB; public static String toReadableSize(long bytes) { if (bytes < KB && bytes >= 0) { return Long.toString(bytes) + " bytes"; }/*ww w .j av a2s. com*/ StringBuilder builder = new StringBuilder(); Formatter format = new Formatter(builder, Locale.getDefault()); if (bytes < MB) { format.format("%.2f KB", (float) bytes / (float) KB); } else if (bytes < GB) { format.format("%.2f MB", (float) bytes / (float) MB); } else if (bytes < TB) { format.format("%.2f GB", (float) bytes / (float) GB); } else { format.format("%.4f TB", (float) bytes / (float) TB); } format.close(); return builder.toString(); } }