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

StringformatSize(long size)
format Size
long SIZE_KB = 1024;
long SIZE_MB = SIZE_KB * 1024;
long SIZE_GB = SIZE_MB * 1024;
if (size < SIZE_KB) {
    return String.format("%d B", (int) size);
} else if (size < SIZE_MB) {
    return String.format("%.2f KB", (float) size / SIZE_KB);
} else if (size < SIZE_GB) {
...
StringformatSize(Long size)
Formats a file size.
if (size == null) {
    return null;
int unit = 1024;
if (size < unit) {
    return String.format("%d B", size);
int exp = (int) (Math.log(size) / Math.log(unit));
...
StringformatSize(long size)
format Size
if (size == -1) {
    return "UNKNOWN";
long kb = size / 1024;
if (kb == 0) {
    return size + "B";
long mb = kb / 1024;
...
StringFormatSize(long size)
Format Size
String suffix = null;
if (size >= 1024) {
    suffix = "KB";
    size /= 1024;
    if (size >= 1024) {
        suffix = "MB";
        size /= 1024;
StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
    resultBuffer.insert(commaOffset, ',');
    commaOffset -= 3;
if (suffix != null) {
    resultBuffer.append(suffix);
return resultBuffer.toString();
StringformatSize(long v)
format Size
if (v < 1024)
    return v + " B";
int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
return String.format("%.1f %sB", (double) v / (1L << (z * 10)), " KMGTPE".charAt(z));
StringformatSize(String strSize)
Formats the given size for display in a directory listing
String strFormattedSize = strSize;
int length = strSize.length();
if (length < 4) {
    strFormattedSize = strSize + "B";
} else if (length >= 4 && length < 7) {
    String strLeft = strSize.substring(0, length - 3);
    String strRight = strSize.substring(length - 3, length - 2);
    StringBuilder buffer = new StringBuilder(strLeft);
...
StringBuilderformatSize(StringBuilder builder, Long size)
format Size
if (size == null) {
    builder.append("???");
    return builder;
int n = 1024 * 1024 * 1024;
int type = 0;
while (size < n && n >= 1024) {
    n /= 1024;
...
StringformatSizeInBytes(int size)
format Size In Bytes
if (size < 1)
    return size + "B";
for (int i = 0; i < (UNITS.length - 1); i++) {
    if (size < UNIT_THRESH[i + 1])
        return (int) (size / UNIT_THRESH[i]) + UNITS[i] + "B";
return size + "B";
StringformatSpaceUsage(long size)
format Space Usage
if (size < 1e4)
    return size + "B";
else if (size < 1e7)
    return "" + Math.round(1D * size / 1024D) + "KB";
else if (size < 1e10)
    return "" + Math.round(1D * size / 1e6) + "MB";
else
    return "" + Math.round(1D * size / 1e9) + "GB";
...
StringformatToolBarDisplay(String text, int size)
format Tool Bar Display
if (text.equals("-1")) {
    text = "Mixed";
int textSize = text.length();
int rem = size - textSize;
if (rem > 0) {
    int newSize = rem / 2;
    for (int i = 0; i < newSize; i++) {
...