Java File Size Readable Format toHumanReadableByteCount(final long bytes)

Here you can find the source of toHumanReadableByteCount(final long bytes)

Description

Returns a human-readable version of the given byte count.

License

Apache License

Parameter

Parameter Description
bytes byte count, may be negative as well.

Return

Byte count in B, KiB, MiB or GiB - including the unit.

Declaration

public static String toHumanReadableByteCount(final long bytes) 

Method Source Code

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

public class Main {
    private static final String[] BYTE_UNITS = { "B", "KiB", "MiB", "GiB" };
    private static final long BYTE_FAC = 1024;

    /**/*from w w w.  j  a v  a  2s.co  m*/
     * Returns a human-readable version of the given byte count.
     * 
     * @param bytes byte count, may be negative as well.
     * @return Byte count in B, KiB, MiB or GiB - including the unit.
     */
    public static String toHumanReadableByteCount(final long bytes) {
        double value = bytes;

        int i = 0;
        for (; Math.abs(value) > BYTE_FAC && i < BYTE_UNITS.length; ++i) {
            value /= BYTE_FAC;
        }

        return ((long) value) + " " + BYTE_UNITS[i];
    }
}

Related

  1. getReadableFileSize(int size)
  2. getReadableFileSize(long fileSizeInBytes)
  3. getReadableFileSize(long size)
  4. getReadableFileSize(long size, boolean abbreviation)
  5. toHuman(Double n)
  6. toHumanReadableFileSize(long fileSize)
  7. toHumanReadableSize(final long bytes)