FileUtilities { public static String fileSizeUnits(final long length) { if (length < BYTES_IN_KILOBYTE) { return length + " B"; } double size = (double) length; NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); DecimalFormat format = (DecimalFormat) nf; format.applyPattern("####0.00"); if (length < BYTES_IN_MEGABYTE) { return format.format(size / BYTES_IN_KILOBYTE) + " KB"; } else if (length < BYTES_IN_GIGABYTE) { return format.format(size / BYTES_IN_MEGABYTE) + " MB"; } else { return format.format(size / BYTES_IN_GIGABYTE) + " GB"; } } private  FileUtilities(); static String getExtension(final String filename); static void clearDirectory(final String directoryPath); static String fileSizeUnits(final long length);  }
@Test public void shouldFormatFileSize() { assertThat(FileUtilities.fileSizeUnits(1), is("1 B")); assertThat(FileUtilities.fileSizeUnits(1024), is("1.00 KB")); assertThat(FileUtilities.fileSizeUnits(1025), is("1.00 KB")); assertThat(FileUtilities.fileSizeUnits(1048576), is("1.00 MB")); assertThat(FileUtilities.fileSizeUnits(1048577), is("1.00 MB")); assertThat(FileUtilities.fileSizeUnits(1073741824), is("1.00 GB")); assertThat(FileUtilities.fileSizeUnits(1073741825), is("1.00 GB")); }