Here you can find the source of getFileSize(File file)
Parameter | Description |
---|---|
file | a parameter |
public static String getFileSize(File file)
//package com.java2s; import java.io.File; import java.text.NumberFormat; public class Main { public static final String[] FILE_SIZE_UNIT = { "Byte", "KB", "MB", "GB", "TB", "PB" }; /**/* w w w . j av a 2s . co m*/ * * @param file * @return the size of file */ public static String getFileSize(File file) { return getFormatSize(file.length()); } /** * * @param size * @return the formatted size */ private static String getFormatSize(double size) { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); int i = 0; String[] unit = FILE_SIZE_UNIT; for (i = 0; i < unit.length; i++) { if ((long) (size / 1024) > 0) { size /= 1024; } else { break; } } return nf.format(size) + unit[i]; } }