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

StringconvertFileSize(double size, Locale locale)
convert File Size
String strSize;
long kb = 1024;
long mb = 1024 * kb;
long gb = 1024 * mb;
long tb = 1024 * gb;
NumberFormat formatter = NumberFormat.getNumberInstance(locale);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
...
StringconvertFileSize(long size)
convert File Size
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
DecimalFormat df = new DecimalFormat("#.00");
if (size >= gb) {
    return df.format((double) size / gb) + "GB";
} else if (size >= mb) {
    return df.format((double) size / mb) + "MB";
...
StringconvertFileSize(long size)
convert File Size
String unit = "B";
if (size >= 1024) {
    size = size / 1024;
    unit = "KB";
if (size >= 1024) {
    size = size / 1024;
    unit = "MB";
...
StringformatarNumeroComZerosAEsquerda(long numero, int size)
formatar Numero Com Zeros A Esquerda
String result = String.valueOf(numero);
while (result.length() < size) {
    result = "0" + result;
return result;
StringformatByteSize(long bytes, int decimals)
format Byte Size
decimals = Math.min(Math.max(decimals, 0), 4);
if (bytes < 1024)
    return bytes + "B";
int exp = (int) (Math.log(bytes) / Math.log(1024));
String pre = "KMGTPE".charAt(exp - 1) + "";
return String.format("%." + decimals + "f%s", bytes / Math.pow(1024, exp), pre);
StringformatByteSize(long bytes, int lastDigits)
This methode formats the bytes in human readable form.
String formated = null;
double work = bytes;
long count = 0;
while (work >= 1024) {
    work /= 1024;
    count++;
formated = String.valueOf(work);
...
StringformatByteSize(long byteSize)
format Byte Size
float size = (float) byteSize;
String metrics = " b";
if (size >= 1024) {
    size = size / 1024;
    metrics = " Kb";
if (size >= 1024) {
    size = size / 1024;
...
StringformatByteSizeToString(long bytes)
Present a size (in bytes) as a human-readable value
int total = sizes.length - 1;
double value = bytes;
for (; value > 1024; total--) {
    value /= 1024;
value = (double) ((int) (value * 10)) / 10;
return value + " " + sizes[total];
StringformatDataSize(long size)
Formats the data size in B, KB, MB or GB, TB as needed
double dsize = size;
String retval = "";
if (dsize < 1024.0) {
    retval = size + " B";
    return retval;
if (dsize < 1024.0 * 1024.0) {
    double val = Math.round(dsize / 1024.0 * 10.0) / 10.0;
...
StringFormatDataSize(long uBytes)
Format Data Size
final long uKB = 1024;
final long uMB = uKB * uKB;
final long uGB = uMB * uKB;
final long uTB = uGB * uKB;
if (uBytes == 0)
    return "0 KB";
if (uBytes <= uKB)
    return "1 KB";
...