Here you can find the source of getFolderSize(File folder)
Parameter | Description |
---|---|
folder | a parameter |
public static String getFolderSize(File folder)
//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" }; /**/*from ww w. j ava 2s . c o m*/ * * @param folder * @return the size of files in the folder, don't include the size of sub-folders */ public static String getFolderSize(File folder) { if (folder == null) return ""; if (folder.isDirectory()) { return getFilesSize(folder.listFiles()); } else { return ""; } } /** * * @param files * File Array * @return the size of files in the file array */ public static String getFilesSize(Object[] files) { if (files == null) return ""; double filesSize = 0; for (Object file : files) { if (((File) file).isFile()) { filesSize += ((File) file).length(); } } return getFormatSize(filesSize); } /** * * @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]; } }