Mega Code Archive

 
Categories / Java Tutorial / File
 

Return readable file size with selected value measure

/**  * Methods for files and folders treatment.  *   * @author Cyril JOUI  */ public final class FileUtil {   /**    * @param fileSize    *          file size to convert and format to string.    * @return return readable file size with selected value measure    */   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());   } }