HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Returning the length of the File in B/KB/MB

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lengthreturningfilethe

Problem

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  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;
}

Solution

Logic

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

if (fileSize > 1024 * 1024)
    doMiB();
} else if (fileSize > 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 > MiB) {
        return format.format(length / MiB) + " MiB";
    }
    if (length > KiB) {
        return format.format(length / KiB) + " KiB";
    }
    return format.format(length) + " B";
}

Code Snippets

if (fileSize > 1024 * 1024)
    doMiB();
} else if (fileSize > 1024) {
    doKiB();
} else {
    doByte();
}
if (!file.isFile()) {
    throw new IllegalArgumentException("Expected argument to be a file");
}
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 > MiB) {
        return format.format(length / MiB) + " MiB";
    }
    if (length > KiB) {
        return format.format(length / KiB) + " KiB";
    }
    return format.format(length) + " B";
}

Context

StackExchange Code Review Q#97798, answer score: 7

Revisions (0)

No revisions yet.