Get readable file size

 
import java.io.File;

public final class Main {
  public static void main(String[] argv){
    System.out.println(getReadableFileSize(10000));
    System.out.println(getReadableFileSize(new File("yourFileName").length()));
  }
  public static String getReadableFileSize(final long fileSize) {
    double l = fileSize * 1D;
    String s = "";
    if (l < 1000) {
      s = fileSize + " B";
    } else {
      l = l / 1024D;
      if (l < 1000) {
        s = l + " KB";
      } else {
        l = l / 1024D;
        if (l < 1000) {
          s = l + " MB";
        } else {
          l = l / 1024D;
          s = l + " GB";
        }
      }
    }
    return (s.length() - s.indexOf('.') <= 3 ? s : s.substring(0, s.indexOf('.') + 3))
        + s.substring(s.indexOf(" "), s.length());
  }

}
  
Home 
  Java Book 
    Runnable examples  

IO File:
  1. Compare File Dates
  2. Compress files using with ZIP
  3. Concatenate files
  4. Copy a File with NIO FileChannel and ByteBuffer
  5. Copy a file with FileReader and FileWriter
  6. Copy a file with InputStream and OutputStream
  7. Copy a file and overwrite
  8. Delete a file
  9. Delete File Recursively
  10. Get readable file size
  11. Move a file
  12. Rename a file
  13. Report a file's status
  14. Search a file by regular expressions
  15. Touch a file: set File Last Modified Time