The method length() in File returns the length of a file in Bytes, this code transforms that into b/kb/mb as can be seen in the Windows ExplorerI ended up with the code below, let me know if it can be optimized or the logic can be improved.public static String getFileSize(File file) {    String modifiedFileSize = null;    double fileSize = 0.0;    if (file.isFile()) {        fileSize = (double) file.length();//in Bytes        if (fileSize < 1024) {            modifiedFileSize = String.valueOf(fileSize).concat(B);        } else if (fileSize > 1024 && fileSize < (1024 * 1024)) {            modifiedFileSize = String.valueOf(Math.round((fileSize / 1024 * 100.0)) / 100.0).concat(KB);        } else {            modifiedFileSize = String.valueOf(Math.round((fileSize / (1024 * 1204) * 100.0)) / 100.0).concat(MB);        }    } else {        modifiedFileSize = Unknown;    }    return modifiedFileSize;}
Returning the length of the File in B/KB/MB
java;performance;file;io;formatting