Java tutorial
//package com.java2s; import java.io.File; import java.text.DecimalFormat; public class Main { private static final String CACHE_MEASURE_FORMATE = "0.0"; private static final int CACHESIZE0 = 1024; private static final int CACHESIZE1 = 1048576; private static final int CACHESIZE2 = 1073741824; private static double mCacheMeasure; /** * * @Title: formatCacheMeasure: format files' measure * @Description: formatCacheMeasure * @param @param path * @param @return * @return String * @throws @param path * @throws @return */ public static String formatCacheMeasure(String path) { String measure; DecimalFormat df = new DecimalFormat(CACHE_MEASURE_FORMATE); getCacheMeasure(path); if (mCacheMeasure == 0.0) { mCacheMeasure = 0; return null; } else if (mCacheMeasure < CACHESIZE1) { measure = df.format((double) mCacheMeasure / CACHESIZE0) + "K"; } else if (mCacheMeasure < 1073741824) { measure = df.format((double) mCacheMeasure / CACHESIZE1) + "M"; } else { measure = df.format((double) mCacheMeasure / CACHESIZE2) + "G"; } mCacheMeasure = 0; return measure; } /** * * @Title: getCacheMeasure: get the measure of all the files under the given * path * @Description: getCacheMeasure * @param @param path: the given file path * @return void * @throws @param path */ private static void getCacheMeasure(String path) { File[] allFiles = new File(path).listFiles(); if (allFiles != null) { for (int i = 0; i < allFiles.length; i++) { if (allFiles[i].isDirectory()) { getCacheMeasure(allFiles[i].getAbsolutePath()); } else { mCacheMeasure += allFiles[i].length(); } } } } }