Returning the length of the File in B/KB/MB 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 Explorer

I 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 &lt; 1024) {
            modifiedFileSize = String.valueOf(fileSize).concat("B");
        } else if (fileSize &gt; 1024 &amp;&amp; fileSize &lt; (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;
}
Logic

You can simplify your logic by testing for the biggest known extension, for example

if (fileSize &gt; 1024 * 1024)
    doMiB();
} else if (fileSize &gt; 1024) {
    doKiB();
} else {
    doByte();
}


Rounding

Don't use Math.round(x / 100.0) * 100. for rounding to two decimal places use the appropriate library function

Argument checking

Don't use default values ("Unknown") for invalid input. If the input to your getFileSize method is not a file, throw an exception

if (!file.isFile()) {
    throw new IllegalArgumentException("Expected argument to be a file");
}


Fast return

This is more personal taste but I prefer to return as soon as possible. If you know the result will be "100.31 KiB" why save it into a local variable instead of returning it immediately?

Conclusion

I would improve the code as followed

private static final DecimalFormat format = new DecimalFormat("#.##");
private static final long MiB = 1024 * 1024;
private static final long KiB = 1024;

public String getFileSize(File file) {

    if (!file.isFile()) {
        throw new IllegalArgumentException("Expected a file");
    }
    final double length = file.length();

    if (length &gt; MiB) {
        return format.format(length / MiB) + " MiB";
    }
    if (length &gt; KiB) {
        return format.format(length / KiB) + " KiB";
    }
    return format.format(length) + " B";
}