Java File Size Get getFileSize(final String filePath)

Here you can find the source of getFileSize(final String filePath)

Description

get File Size

License

Open Source License

Declaration

public static String getFileSize(final String filePath) 

Method Source Code


//package com.java2s;

import java.io.File;

public class Main {
    public static String getFileSize(final String filePath) {
        String formattedFileSize = "0 B";
        File fileReference = new File(filePath);
        if (fileReference.exists()) {
            long fileSizeInBytes = fileReference.length();
            if (fileSizeInBytes < 1024) {
                return fileSizeInBytes + " B";
            }/*from  w w  w.  j ava  2 s  . c  om*/
            int unitIndex = (int) (Math.log(fileSizeInBytes) / Math.log(1024));
            String computedUnit = "KMGT".charAt(unitIndex - 1) + "";
            formattedFileSize = String.format("%.1f %sB", fileSizeInBytes / Math.pow(1024, unitIndex),
                    computedUnit);
        }
        return formattedFileSize;
    }
}

Related

  1. getFileSize(File file)
  2. getFileSize(File file)
  3. getFileSize(File file)
  4. getFileSize(File[] files)
  5. getFileSize(final String filename)
  6. getFileSize(final String path)
  7. getFileSize(InputStream inputStream)
  8. getFileSize(int fileSize)
  9. getFileSize(long fileSize)