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

intformatSize(Integer size)
format Size
return formatPositiveInt(size, 10);
StringformatSize(long bytes)
format Size
long factor = 1;
int pos = 0;
String[] abrv = { "B", "KB", "MB", "GB" };
for (int i = 0; i < 4; i++) {
    if (bytes / factor == 0) {
        pos = i - 1;
        break;
    factor *= 1024;
return "" + ((bytes * 1024) / factor) + " " + abrv[pos];
StringformatSize(long bytes, String b, String kb, String mb, String gb)
format Size
if (bytes > 16L * G)
    return (bytes / G) + gb;
else if (bytes > 16L * M)
    return (bytes / M) + mb;
else if (bytes > 16L * K)
    return (bytes / K) + kb;
else
    return bytes + b;
...
StringformatSize(long size)
Change byte to KB/MB/GB...(Accurate to two decimal places)
if (size <= 0) {
    return "0B";
String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "NB", "DB", "CB", };
int i = 0;
float newSize = size;
while (newSize >= 1024) {
    newSize /= 1024;
...
StringformatSize(long size)
Returns a human-readable version of the file size.
String display;
if (size / ONE_G > 0) {
    display = String.format("%.2f", (size + 0.0) / ONE_G) + " G";
} else if (size / ONE_M > 0) {
    display = String.format("%.2f", (size + 0.0) / ONE_M) + " M";
} else if (size / ONE_K > 0) {
    display = String.format("%.2f", (size + 0.0) / ONE_K) + " K";
} else {
...
StringformatSize(long size)
format Size
if (size < GIGA)
    return ((float) size / MEGA) + "MB";
else
    return ((float) size / GIGA) + "GB";
StringformatSize(long size)
Takes a size of a file or heap of memory in the form of a long and returns a formatted readable version in the form of byte units.
if (size < 1024) {
    return size + " B";
int exp = (int) (Math.log(size) / Math.log(1024));
return String.format("%.1f %sB", size / Math.pow(1024, exp), "KMGTPE".charAt(exp - 1));
StringformatSize(long size)
format Size
String result = "";
if (size >= 0 && size < KB) {
    result = size + "B";
} else if (size >= KB && size < MB) {
    result = size / KB + "KB";
} else if (size >= MB && size < GB) {
    result = size / MB + "MB";
} else if (size >= GB) {
...
StringformatSize(long size)
format Size
if (size < 1024)
    return String.format("%d B", size);
else if (size < 1024 * 1024)
    return String.format("%.2f KB", (double) size / 1024);
else if (size < 1024 * 1024 * 1024)
    return String.format("%.2f MB", (double) size / (1024 * 1024));
else if (size < 1024L * 1024 * 1024 * 1024)
    return String.format("%.2f GB", (double) size / (1024 * 1024 * 1024));
...
StringformatSize(long size)
format Size
if (size >= 100 * ONE_GIGABYTE)
    return String.format("%,d GB", size / ONE_GIGABYTE);
if (size >= 10 * ONE_GIGABYTE)
    return String.format("%.1f GB", (double) size / ONE_GIGABYTE);
if (size >= ONE_GIGABYTE)
    return String.format("%.2f GB", (double) size / ONE_GIGABYTE);
if (size >= 10 * ONE_MEGABYTE)
    return String.format("%,d MB", size / ONE_MEGABYTE);
...