Java Size Format formatFileSize(long size)

Here you can find the source of formatFileSize(long size)

Description

format File Size

License

Open Source License

Declaration

public static String formatFileSize(long size) 

Method Source Code

//package com.java2s;
/*/*www.j av  a  2  s.c  om*/
 *  Gruntspud
 *
 *  Copyright (C) 2002 Brett Smith.
 *
 *  Written by: Brett Smith <t_magicthize@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 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
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    private final static NumberFormat FILE_SIZE_FORMAT = new DecimalFormat();

    /**
     *
     */
    public static String formatFileSize(long size) {
        double bytes = (double) size;

        if (bytes < 1024) {
            return String.valueOf((int) bytes) + " bytes";
        } else if (bytes < 1048576) {
            return FILE_SIZE_FORMAT.format(bytes / 1024) + " K";
        } else {

            return FILE_SIZE_FORMAT.format(bytes / 1024 / 1024) + " MB";
        }
    }
}

Related

  1. formatFileSize(long fileS)
  2. formatFilesize(long filesize)
  3. formatFileSize(long fileSize, int decimalPos)
  4. formatFileSize(long length)
  5. formatFileSize(long size)
  6. formatFileSize(long size)
  7. formatFileSize(long size)
  8. formatFileSize(long size)
  9. formatFileSize(Long sizeBytes)