Skip to content

Instantly share code, notes, and snippets.

@ozodrukh
Created April 9, 2015 17:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ozodrukh/6d74f8aacf74ad712e3c to your computer and use it in GitHub Desktop.
Some useful file utility methods
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment