Here you can find the source of formatBytes(long bytes)
public static String formatBytes(long bytes)
//package com.java2s; /*//from w w w. j a va 2 s.com * Copyright (c) 2008-2011 by Jan Stender, Bjoern Kolbeck, * Zuse Institute Berlin * * Licensed under the BSD License, see LICENSE file for details. * */ public class Main { public static String formatBytes(long bytes) { double kb = bytes / 1024.0; double mb = bytes / (1024.0 * 1024.0); double gb = bytes / (1024.0 * 1024.0 * 1024.0); double tb = bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0); if (tb >= 1.0) { return String.format("%.2f TB", tb); } else if (gb >= 1.0) { return String.format("%.2f GB", gb); } else if (mb >= 1.0) { return String.format("%.2f MB", mb); } else if (kb >= 1.0) { return String.format("%.2f kB", kb); } else { return bytes + " bytes"; } } }