Java File Size Readable Format convertFileSize(long size)

Here you can find the source of convertFileSize(long size)

Description

convert File Size

License

Open Source License

Declaration

public static String convertFileSize(long size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    private static final NumberFormat numberFormat = new DecimalFormat(
            "#,###.##");

    public static String convertFileSize(long size) {
        String unit = "B";
        if (size >= 1024) {
            size = size / 1024;//from   w  w  w  . j  a v  a2s. c  o m
            unit = "KB";
        }
        if (size >= 1024) {
            size = size / 1024;
            unit = "MB";
        }
        if (size >= 1024) {
            size = size / 1024;
            unit = "GB";
        }
        if (size >= 1024) {
            size = size / 1024;
            unit = "TB";
        }
        return numberFormat.format(size) + unit;
    }
}

Related

  1. convertFileSize(double size, Locale locale)
  2. convertFileSize(long size)
  3. formatarNumeroComZerosAEsquerda(long numero, int size)
  4. formatByteSize(long bytes, int decimals)
  5. formatByteSize(long bytes, int lastDigits)