Java Utililty Methods File Size Readable Format

List of utility methods to do File Size Readable Format

Description

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

Method

longgetFileSizeInByte(File file)
get File Size In Byte
return file.length();
longgetFileSizeInBytes(File f)
get File Size In Bytes
long size = 0;
if (f.isFile()) {
    size = f.length();
} else if (f.isDirectory()) {
    File[] files = f.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isFile()) {
            size += files[i].length();
...
floatgetFileSizeInMB(String fileName)
get File Size In MB
float ret = getFileSizeInBytes(fileName);
ret = ret / (float) (1024 * 1024);
return ret;
StringgetHumanSize(File dir)
get Human Size
return getHumanSize(getSize(dir));
StringgetReadableFileSize(int size)
Get the file size in a human-readable string.
final int BYTES_IN_KILOBYTES = 1024;
final DecimalFormat dec = new DecimalFormat("###.#");
final String KILOBYTES = " KB";
final String MEGABYTES = " MB";
final String GIGABYTES = " GB";
float fileSize = 0;
String suffix = KILOBYTES;
if (size > BYTES_IN_KILOBYTES) {
...
StringgetReadableFileSize(long fileSizeInBytes)
Returns a human readable representation of a filesize given in bytes
if (fileSizeInBytes <= 0) {
    return "0";
int digitGroups = (int) (Math.log10(fileSizeInBytes) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(fileSizeInBytes / Math.pow(1024, digitGroups)) + " "
        + units[digitGroups];
StringgetReadableFileSize(long size)
get Readable 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];
StringgetReadableFileSize(long size, boolean abbreviation)
Format filesize to human readable format Get size in bytes e.g.
if (size <= 0) {
    return "0B";
String[] units = abbreviation ? new String[] { "B", "kB", "MB", "GB", "TB" }
        : new String[] { "Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes" };
int unit = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, unit)) + " " + units[unit];
StringtoHuman(Double n)
prints large numbers as KB, MB, GB or TB
if (n == null)
    return null;
String u = "B";
if (n > 1024) {
    n /= 1024;
    u = "KB";
if (n > 1024) {
...
StringtoHumanReadableByteCount(final long bytes)
Returns a human-readable version of the given byte count.
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];