1

在下面的程序中,我插入

RThread 类中 allFiles HashMap 中的 files[i].lastModified() 值

当我试图从主类中的 allFiles HashMap 中检索 lastModified 的值时,该值将变为 null。因此,我将 File 对象直接放在 HashMpa 中,而不是 lastModified 值,然后它就起作用了。

任何人都可以指导我这背后的概念。

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

public class RCFLister {

    ConcurrentHashMap allFiles = new ConcurrentHashMap();

    String fileType = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
        RCFLister rcFileLister = new RCFLister();
        rcFileLister.recentFiles(args);
    }

    public void recentFiles(String[] args) {

        int days = 1;
        ThreadGroup rootthreadGr = new ThreadGroup("rootsThreadGroup");
        // reading number of days
        if (args.length > 0 && args[0] != null) {
            days = Integer.valueOf(args[0]);
        }
        // reading file type
        if (args.length > 0 && args[0] != null) {
            fileType = args[1];
        }
        fileType = "png";
        // fileextFilter = new FileExtensionFilter(fileType);
        File[] roots = File.listRoots();
        List threads = new ArrayList();
        for (int i = 0; i < roots.length; i++) {
            // System.out.println(roots[i].getAbsolutePath());
            // rfThread = null;

            RThread rfThread = new RThread(roots[i]);
            Thread t = new Thread(rfThread);
            t.start();
            threads.add(t);
        }

        for (int i = 0; i < threads.size(); i++) {
            try {
                ((Thread) threads.get(i)).join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   

        ValueComparator vc = new ValueComparator(allFiles);
        TreeMap sortedMap = new TreeMap(vc);
        sortedMap.putAll(allFiles);
        System.out.println(sortedMap.size());
        Iterator it = sortedMap.keySet().iterator();
        while (it.hasNext()) {
            Object key = it.next();
            System.out.println(key + " " + sortedMap.get(key));
        }
    }

    private class ValueComparator implements Comparator {

        Map allFiles;

        public ValueComparator(Map allFiles) {
            this.allFiles = allFiles;
        }

        public int compare(Object o1, Object o2) {
            if (((Long) allFiles.get(o1)) >= ((Long) allFiles.get(o2))) {
                return -1;
            } else {
                return 1;
            }
        }
    }

    private class RThread implements Runnable {
        File rootFolder;



        RThread(File root) {
            rootFolder = root;          
        }

        public void run() {
            getFiles(rootFolder);

        }

        public void getFiles(File folder) {
            File[] files = folder.listFiles();
            for (int i = 0; files != null && i < files.length; i++) {
                // System.out.println(files[i].getAbsolutePath());
                if (files[i].isDirectory()) {
                    getFiles(files[i]);
                } else if (fileType == null
                        || (fileType != null && files[i].getName().endsWith(
                                fileType))) {
                    String filename = null;
                    try {
                        filename = files[i].getCanonicalPath();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                                                 if(files[i].lastModified()!=0)
                    allFiles.put(filename, files[i].lastModified());
                }
            }
        }

    }

}
4

1 回答 1

0

ValueComparator 的 compare() 方法从不返回 0。返回 0 表示缺少相等性,因此即使存在键也找不到它。键也是字符串(文件名),您正在按值(长)进行比较。需要重新考虑如何存储数据。只是为了确保它是唯一的问题,打印 CocurrentHashMap。

一定要使用 Generic,它一开始看起来很乏味,但实际上并没有那么糟糕。它将有助于在没有警告的情况下编译代码并实现类型安全。

于 2012-12-28T05:39:44.683 回答