Java File Size Get readableFileSize(long size)

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

Description

readable File Size

License

Apache License

Declaration

public static String readableFileSize(long size) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;

public class Main {
    final static String[] units = new String[] { "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };

    public static String readableFileSize(long size) {
        if (size <= 0)
            return "0";
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }/*from ww  w  .  j a  va  2s.c o m*/

    public static String readableFileSize(double size) {
        if (size <= 0) {
            return "0";
        }
        if (size > Math.pow(2, 89)) {
            return "gt 512 YB !";
        }
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
}

Related

  1. getStringSizeLengthFile(long size)
  2. humanFileSize(Long longFileSize)
  3. humanReadableFileSize(long size)
  4. prettyFileSize(long size)
  5. readableFileSize(final long size)
  6. readableFileSize(long size)
  7. stringifyFileSize(double value)
  8. toFileSize(final long longSize, final int decimalPos)