Find top 10 large files

package yxc.citi.file.find;


import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


/*********************************************************************************
//* Filename:      FindTopLargeFile.java
//* Revision:      1.0
//* Author:        <yao xiucai>
//* Created On:    2014年7月29日
//* Modified by:   
//* Modified On:   
//*
//* Description:   <find top ${topNum} large file>
/********************************************************************************/


public class FindTopLargeFile {


    //top large file's num
    private static int                 topNum        = 10;
    //test Directory
    private static String              fileDirectory = "E:/tmp";


    private static Map<Double, String> fileMap       = null;


    /**
     * get file size
     * @param file
     * @return
     * @throws Exception
     */
    public static double getFileSize(File file) throws Exception {
        double size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
            fis.close();
        }
        else {
            return 0;
        }
        size = size / 1024.00;
        size = Double.parseDouble(size + "");
        BigDecimal b = new BigDecimal(size);
        double y1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        return Double.parseDouble(df.format(y1));
    }


    /**
     * find all files
     * @param dir
     * @throws Exception
     */
    final static Map<Double, String> findAllFiles(File dir) throws Exception {
        File[] fs = dir.listFiles();
        for (int i = 0 ; i < fs.length ; i++) {
            if (fs[i].isDirectory()) {
                try {
                    findAllFiles(fs[i]);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else {
                Double key = getFileSize(fs[i]);
                String value = fs[i].getAbsolutePath();
                if (fileMap.containsKey(key)) {
                    key = key + 0.0001; //解决冲突。有效数位小数点后两位,因此最大支持冲突数位100
                    fileMap.put(key, value);
                }
                else {
                    fileMap.put(key, value);
                }
            }
        }


        return fileMap;
    }


    /**
     * print a map's value
     * @param map
     */
    final static void printDescMap(Map<Double, String> map) {
        if (map != null && map.size() > 0) {
            int fileNum = 0;
            Set<Double> keySet = map.keySet();
            Iterator<Double> iter = keySet.iterator();
            while (iter.hasNext()) {
                fileNum = fileNum + 1;
                Double key = iter.next();
                String value = map.get(key);
                DecimalFormat df = new DecimalFormat("0.00");
                String keyStr = df.format(key);
                System.out.println(value + " : " + keyStr + " KB");


                if (fileNum == topNum) {
                    break;
                }
            }
        }
        else {
            System.out.println("no file!");
        }
    }


    /**
     * 1.find all files,and get the result map;
     * 2.print result map
     * @param filePath
     * @throws Exception
     */
    public static void sortByDesc(String filePath) throws Exception {
        File file = new File(filePath);
        fileMap = new TreeMap<Double, String>(new Comparator<Double>() {


            public int compare(Double obj1, Double obj2) {
                //desc sort
                return obj2.compareTo(obj1);
            }
        });
        if (!file.exists()) {
            System.out.println("The file does not exist:" + filePath);
        }
        else {
            findAllFiles(file);
            printDescMap(fileMap);
        }


    }


    //test method
    public static void main(String[] args) throws Exception {
        long begin = System.currentTimeMillis();


        sortByDesc(fileDirectory);


        long end = System.currentTimeMillis();


        System.out.println("time:" + (end - begin) + " ms");
    }
}

猜你喜欢

转载自blog.csdn.net/xiucaiyao/article/details/38268537