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

StringFormatDataSizeKB(long uBytes)
Format Data Size KB
final long uKB = 1024;
if (uBytes == 0)
    return "0 KB";
if (uBytes <= uKB)
    return "1 KB";
return (((uBytes - 1L) / uKB) + 1L) + " KB";
StringformatFileSize(long bytes)
format File Size
if (bytes < 4096) {
    return bytes + " bytes";
} else if (bytes < 1024 * 1024) {
    return (Math.round(10 * bytes / 1024) / 10) + " kb";
} else {
    return (Math.round(10 * bytes / (1024 * 1024)) / 10) + " mb";
StringformatFileSize(long bytes, boolean si)
format File Size
int unit = si ? 1000 : 1024;
if (bytes < unit)
    return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
StringformatFileSize(long fileSize)
Formats fileSize (in byte) in B/KB/MB/GB
String unit = "B";
double size = 0;
if (fileSize < 1024) {
    unit = "B";
    size = (double) fileSize;
} else if (fileSize < (1024 * 1024L)) {
    unit = "KB";
    size = (double) fileSize / (1024L);
...
StringformatFileSize(long fileSize)
Formats a file size as a string (Kb).
long size = fileSize / 1024;
return String.valueOf(size);
StringformatFileSize(long fileSize)
format File Size
String suffix = null;
if (fileSize < 1024L)
    suffix = "B";
else if (fileSize < 0x100000L) {
    fileSize = Math.round(fileSize / 1024L);
    suffix = "KB";
} else {
    fileSize = Math.round((fileSize / 0x100000L) * 100L) / 100;
...
StringformatFileSize(long fileSizeLong)
format File Size
double fileSize = fileSizeLong;
if (fileSize < 1024) {
    return String.format("%d bytes", fileSizeLong);
fileSize /= 1024;
if (fileSize < 1024) {
    return String.format("%.2f KB", fileSize);
fileSize /= 1024;
return String.format("%.2f MB", fileSize);
StringformatFileSize(long size)
Formats a file size to a printable string.
float value = size;
String unit;
if (size > 1000000) {
    value /= 1048576;
    unit = " MiB";
} else if (size > 2000) {
    value /= 1024;
    unit = " KiB";
...
StringformatFileSize(long size)
Formats file size, applying KB, MB, GB units.
if (size == 0) {
    return "0";
if (size < 1024) {
    return size + "B";
if (size >= 1024 && size < 1048576) {
    return Math.round((size / 1024) * 100.0) / 100.0 + "KB";
...
StringformatFileSize(long size, String format)
1204,K => 1K
if (format.equals("K")) {
    return size / 1024.0 + "K";
if (format.equals("M")) {
    return size / 1024.0 / 1024.0 + "M";
if (format.equals("G")) {
    return size / 1024.0 / 1024.0 / 1024.0 + "G";
...