Java File Size Get getFileSize(File file)

Here you can find the source of getFileSize(File file)

Description

get File Size

License

Open Source License

Parameter

Parameter Description
file a parameter

Return

the size of file

Declaration

public static String getFileSize(File file) 

Method Source Code


//package com.java2s;

import java.io.File;

import java.text.NumberFormat;

public class Main {
    public static final String[] FILE_SIZE_UNIT = { "Byte", "KB", "MB", "GB", "TB", "PB" };

    /**/* w  w  w .  j  av a 2s  .  co  m*/
     * 
     * @param file
     * @return the size of file
     */
    public static String getFileSize(File file) {
        return getFormatSize(file.length());
    }

    /**
     * 
     * @param size
     * @return the formatted size
     */
    private static String getFormatSize(double size) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(2);
        int i = 0;
        String[] unit = FILE_SIZE_UNIT;
        for (i = 0; i < unit.length; i++) {
            if ((long) (size / 1024) > 0) {
                size /= 1024;
            } else {
                break;
            }
        }
        return nf.format(size) + unit[i];
    }
}

Related

  1. getFileSize(File file)
  2. getFileSize(File file)
  3. getFileSize(File file)
  4. getFileSize(File file)
  5. getFileSize(File file)
  6. getFileSize(File file)
  7. getFileSize(File[] files)
  8. getFileSize(final String filename)
  9. getFileSize(final String filePath)