Java File Size Readable Format formatFileSize(long size, String format)

Here you can find the source of formatFileSize(long size, String format)

Description

1204,K => 1K

License

Apache License

Parameter

Parameter Description
size a parameter
format K,M,G,T

Declaration

public static String formatFileSize(long size, String format) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static String formatFileSize(long size, String format) {

        if (format.equals("K")) {
            return size / 1024.0 + "K";
        }//from w ww. ja  v a 2s  . c om

        if (format.equals("M")) {
            return size / 1024.0 / 1024.0 + "M";
        }

        if (format.equals("G")) {
            return size / 1024.0 / 1024.0 / 1024.0 + "G";
        }

        if (format.equals("T")) {
            return size / 1024.0 / 1024.0 / 1024.0 / 1024.0 + "T";
        }

        return size + "B";
    }
}

Related

  1. formatFileSize(long fileSize)
  2. formatFileSize(long fileSize)
  3. formatFileSize(long fileSizeLong)
  4. formatFileSize(long size)
  5. formatFileSize(long size)
  6. FormatFileSize(String filesize)
  7. formatFilesize(String filesize, int decimalPlaces)
  8. formatFileStats(final String label, final long fileCount, final Object rawSize)
  9. formatGroup(String str, int groupSize, int lineBreak)