Explain Codes LogoExplain Codes Logo

How can I convert byte size into a human-readable format in Java?

java
byte-conversion
human-readable-format
java-utility-methods
Alex KataevbyAlex Kataev·Feb 9, 2025
TLDR

The method below swiftly converts bytes into a human-readable format:

public static String humanReadableByteCount(long bytes) { int unit = 1024; if (bytes < unit) return bytes + " B"; // B for bytes, not B for bees 🐝. int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = "KMGTPE".charAt(exp-1) + "i"; // Kids, remember when gauging data, P comes after T. return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); }

Math.log is the "measuring tape," determining the power, while String.format adjusts the decimal places and assigns the correct unit.

Juggling between SI and binary units

Different protocols use either SI units (base 10) or binary units (base 2), and a good byte conversion method handles both:

// SI Units: where Kilobyte is a posh way of saying 1000 bytes public static String humanReadableByteCountSI(long bytes) { long absBytes = Math.abs(bytes); if (absBytes < 1000) return bytes + " B"; // Bytes, still learning to count. int exp = (int) (Math.log(absBytes) / Math.log(1000)); char pre = "kMGTPE".charAt(exp - 1); return String.format("%.1f %cB", absBytes / Math.pow(1000, exp), pre); // Don't mess up the e's and the i's } // Binary Units: where Kibibyte sounds delicious, but is just 1024 bytes public static String humanReadableByteCountBin(long bytes) { long absBytes = Math.abs(bytes); if (absBytes < 1024) return bytes + " B"; // Bytes, the little ones in the family. int exp = (int) (Math.log(absBytes) / Math.log(1024)); char pre = "KMGTPE".charAt(exp - 1); return String.format("%.1f %ciB", absBytes / Math.pow(1024, exp), pre); // The secret i… }

For SI unit conversions, use humanReadableByteCountSI and for binary unit conversions, employ humanReadableByteCountBin. Both methods embrace %c and %f to hit bullseye on precision and human-readability.

Wrestling with big bytes and tiny bits

Whether you're working with petabytes of big data or just a few bytes, size formatting should hold up. Here's how:

public static String bytesIntoHumanReadable(long bytes) { if (bytes == Long.MIN_VALUE) return "8 EiB"; // Because EiB, the Hulk 💪 of data, needs special care. long absBytes = Math.abs(bytes); if (absBytes < 1024) return bytes + " B"; int lz = Long.numberOfLeadingZeros(absBytes); int shift = Math.max(0, (63 - lz) / 10); char unit = " KMGTPE".charAt(shift); absBytes = absBytes >> (shift * 10); return String.format("%,d %cB", absBytes, unit); // Who needs MB when you have B in billions! }

The method employs Long.numberOfLeadingZeros() and bit shifting (>>) to make efficient and accurate byte division. Plus, there's a special red carpet treatment for Long.MIN_VALUE!

Beautifying the byte conversion output

The spotlight on DecimalFormat can help make your byte conversion output more presentable:

public static String formatReadableSize(long bytes) { DecimalFormat df = new DecimalFormat("#,##0.#"); // It's not vanity, it's vanity sizing! int unit = 1000; if(bytes < unit) return df.format(bytes) + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = "kMGTPE".charAt(exp - 1) + (unit == 1000 ? "" : "i"); return df.format(bytes / Math.pow(unit, exp)) + " " + pre + "B"; }

The method utilizes DecimalFormat to give byte conversions a makeover, including comma delineation and precision.

An antidote to negativity

Negative byte values can be troublesome. Here's a defensive coding snippet to ensure readable output in such cases:

public static String readableByteSize(long bytes) { if (bytes < 0) { // If bytes decide to go rogue… return "-" + readableByteSize(-bytes); // Bounce back with style! } // Rest of the method here... }

The might of libraries

Libraries such as Google Guava or Apache Commons IO can be a quick perk for handling bytes:

String displaySize = FileUtils.byteCountToDisplaySize(1024); // Yes, Apache got it handled!

Using libraries can offer more reliable, precise, and sometimes faster solutions.