Java Size Format formatFilesize(final long filesize, final Locale locale)

Here you can find the source of formatFilesize(final long filesize, final Locale locale)

Description

Returns the formatted file size to Bytes, KB, MB or GB depending on the given value.

License

Open Source License

Parameter

Parameter Description
filesize in bytes
locale the locale to translate the result to (e.g. in France they us

Return

the formatted filesize to Bytes, KB, MB or GB depending on the given value.

Declaration

public static String formatFilesize(final long filesize, final Locale locale) 

Method Source Code

//package com.java2s;
/*/*from  ww  w. ja  v a2s. c  o m*/
 * FileUtil.java, helpers for disk I/O.
 * Copyright (C) 2001 - 2011 Achim Westermann.
 * 
 * This library 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 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * If you modify or optimize the code in a useful way please let me know.
 * Achim.Westermann@gmx.de
 */

import java.text.MessageFormat;
import java.util.Locale;

import java.util.ResourceBundle;

public class Main {
    /** Needed for localization. */
    private static final ResourceBundle m_bundle = ResourceBundle.getBundle("messages");

    /**
     * Returns the formatted file size to Bytes, KB, MB or GB depending on the
     * given value.
     * <p>
     * 
     * @param filesize
     *          in bytes
     * 
     * @param locale
     *          the locale to translate the result to (e.g. in France they us
     * 
     * @return the formatted filesize to Bytes, KB, MB or GB depending on the
     *         given value.
     */
    public static String formatFilesize(final long filesize, final Locale locale) {

        String result;
        final long filesizeNormal = Math.abs(filesize);

        if (Math.abs(filesize) < 1024) {
            result = MessageFormat.format(m_bundle.getString("GUI_FILEUTIL_FILESIZE_BYTES_1"),
                    new Object[] { new Long(filesizeNormal) });
        } else if (filesizeNormal < 1048576) {
            // 1048576 = 1024.0 * 1024.0
            result = MessageFormat.format(m_bundle.getString("GUI_FILEUTIL_FILESIZE_KBYTES_1"),
                    new Object[] { new Double(filesizeNormal / 1024.0) });
        } else if (filesizeNormal < 1073741824) {
            // 1024.0^3 = 1073741824
            result = MessageFormat.format(m_bundle.getString("GUI_FILEUTIL_FILESIZE_MBYTES_1"),
                    new Object[] { new Double(filesize / 1048576.0) });
        } else {
            result = MessageFormat.format(m_bundle.getString("GUI_FILEUTIL_FILESIZE_GBYTES_1"),
                    new Object[] { new Double(filesizeNormal / 1073741824.0) });
        }
        return result;
    }
}

Related

  1. formatDataSize(long bytes)
  2. formatDecimal(double size)
  3. formatDiskSize(final long diskSize)
  4. formatFileSize(double fileSize, int precision, int unit, boolean showUnit)
  5. formatFileSize(File file)
  6. formatFilesize(int s)
  7. formatFileSize(long fileS)
  8. formatFilesize(long filesize)
  9. formatFileSize(long fileSize, int decimalPos)