Java Utililty Methods File Size Get

List of utility methods to do File Size Get

Description

The list of methods to do File Size Get are organized into topic(s).

Method

StringprettyFileSize(long size)
pretty File Size
if (size <= 0)
    return "0";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
StringreadableFileSize(final long size)
readable File Size
if (size < 0)
    return "";
if (size == 0)
    return "0.0";
final int digitGroup = (int) (Math.log10(size) / Math.log10(1024));
return DECIMAL_FORMAT.format(size / Math.pow(1024, digitGroup)) + " " + UNITS[digitGroup];
StringreadableFileSize(long size)
readable File 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];
StringreadableFileSize(long size)
readable File Size
if (size <= 0)
    return "0 B";
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)).replace(",", ".") + " "
        + UNITS[digitGroups];
StringstringifyFileSize(double value)
stringify File Size
double d;
if (value >= TB) {
    d = value / TB;
    String val = df.format(d);
    return val + " TB";
} else if (value >= GB) {
    d = value / GB;
    String val = df.format(d);
...
StringtoFileSize(final long longSize, final int decimalPos)
to File Size
final NumberFormat fmt = NumberFormat.getNumberInstance();
if (decimalPos >= 0) {
    fmt.setMaximumFractionDigits(decimalPos);
final double size = longSize;
double val = size / (1024 * 1024);
if (val > 1) {
    return fmt.format(val).concat(" MB");
...