Java File Size Get getFileSize(String filename)

Here you can find the source of getFileSize(String filename)

Description

get File Size

License

Open Source License

Declaration

public static String getFileSize(String filename) 

Method Source Code

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

import java.io.*;
import java.text.DecimalFormat;

public class Main {
    public static String getFileSize(String filename) {
        File file = new File(filename);
        return getReadableFileSize(file.length());
    }//from   ww w.j  a v  a2  s  .  c o m

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

Related

  1. getFileSize(final String path)
  2. getFileSize(InputStream inputStream)
  3. getFileSize(int fileSize)
  4. getFileSize(long fileSize)
  5. getFilesize(String address)
  6. getFileSize(String fileName)
  7. getFileSize(String filename)
  8. getFileSize(String fileName, Logger log)
  9. getFileSize(String filename, String dir)