Android File Size Readable Format fileSizeFormat(long length)

Here you can find the source of fileSizeFormat(long length)

Description

file Size Format

License

Open Source License

Declaration

public static String fileSizeFormat(long length) 

Method Source Code

//package com.java2s;
/**/*from w w  w.j a  v  a2 s .co  m*/
 * Android Micro Web Server
 * Copyright (C) 2011  ScR4tCh
 * Contact scr4tch@scr4tch.org

 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String fileSizeFormat(long length) {
        return fileSizeFormat(length, 0);
    }

    private static String fileSizeFormat(long length, int lev) {
        if (length > 1024 && lev < 6) {
            return fileSizeFormat(length / 1024, lev + 1);
        } else {
            switch (lev) {
            case 0:
                return "" + length;
            case 1:
                return length + "K";
            case 2:
                return length + "M";
            case 3:
                return length + "G";
            case 4:
                return length + "T";
            default:
                return length + "E";
            }
        }
    }
}

Related

  1. getSizeInKbytes(final File path)
  2. getSizeInMegabytes(final File path)
  3. formatFileSize(long fileLength)
  4. fileSizeFormat(long length, int lev)
  5. FormetFileSize(long fileS)