Java File Size Get humanReadableFileSize(long size)

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

Description

human Readable File Size

License

Open Source License

Declaration

public static String humanReadableFileSize(long size) 

Method Source Code


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

import java.text.DecimalFormat;

public class Main {
    public static String humanReadableFileSize(long size) {
        if (size <= 0) {
            return "0";
        }/* ww  w.  j ava  2  s.  c o  m*/

        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int magnitude = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, magnitude)) + " " + units[magnitude];
    }
}

Related

  1. getFolderSize(File folder)
  2. getSize(long fileSize)
  3. getSizeErrorMessage(String pattern, Long maxFileSize, Long fileSize, String fileName)
  4. getStringSizeLengthFile(long size)
  5. humanFileSize(Long longFileSize)
  6. prettyFileSize(long size)
  7. readableFileSize(final long size)
  8. readableFileSize(long size)
  9. readableFileSize(long size)