FileUtil { public static String formatFileSize(long fileSize, int decimalPos) { NumberFormat fmt = NumberFormat.getNumberInstance(); if (decimalPos >= 0) { fmt.setMaximumFractionDigits(decimalPos); } String formattedSize; final double size = fileSize; double val = size / (BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE); if (val > 1) { formattedSize = fmt.format(val).concat(" GB"); } else { val = size / (BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE); if (val > 1) { formattedSize = fmt.format(val).concat(" MB"); } else { val = size / BYTES_IN_KILOBYTE; if (val > 1) { formattedSize = fmt.format(val).concat(" KB"); } else { formattedSize = fmt.format(size).concat(" bytes"); } } } return formattedSize; } private  FileUtil(); static boolean isSymbolicLink(final Path file); static String formatFileSize(long fileSize, int decimalPos); static String fileName(final Path path); static FileTime lastModifiedQuietly(final Path path); static long sizeQuietly(final Path path); static boolean deleteQuietly(final Path path); static boolean mkdirsQuietly(final Path dir); static List<Path> listFilesQuietly(final Path path, final boolean recursive, final FileFilter fileFilter); static List<Path> listFiles(final Path path, final boolean recursive, final FileFilter fileFilter); static List<Path> listFilesQuietly(final Path path, final boolean recursive, final DirectoryStream.Filter<Path> filter); static List<Path> listFiles(final Path path, final boolean recursive, final DirectoryStream.Filter<Path> filter);  }
@Test public void testFormatFileSize() { final long kb12 = 12L * 1024L; final long mb12 = 12L * 1000L * 1024L; final long gb12 = 12L * 1024L * 1024L * 1024L; final long tb12 = 12L * 1024L * 1024L * 1024L * 1024L; DecimalFormatSymbols dfs = new DecimalFormatSymbols(); assertEquals("12 bytes", FileUtil.formatFileSize(12L, 3)); assertEquals("12 KB", FileUtil.formatFileSize(kb12, 3)); assertEquals("11"+dfs.getDecimalSeparator()+"7 MB", FileUtil.formatFileSize(mb12, 1)); assertEquals("12 GB", FileUtil.formatFileSize(gb12, 3)); assertEquals("12"+dfs.getGroupingSeparator()+"288 GB", FileUtil.formatFileSize(tb12, 3)); }