ListFiles performance vs file system visitor

Paulo Levi i30817 at gmail.com
Mon Feb 16 13:16:40 PST 2009


Hi. I saw recently that the nio2 was going to have a new visitor like
implementation for filesystem walking and i am wondering if is faster
than doing a recursive list files by hand. I'm my application this is
the main bottleneck.

Also besides this question, i hope you don't think it too cheeky for
me to post my function here for advice. Windows takes about 6 seconds
find the files (in the explorer given a * regex) , java 49 seconds,
This on a usb port. On a usb 2 port times become 3s windows and 21
seconds java.

    public static void getFiles(int levels, File[] sum, List<File>
files, List<File> directories) {
        int dirIndex = directories.size();


        for (File f : sum) {
            if (f.isDirectory()) {
                directories.add(f);
            } else {
                files.add(f);
            }
        }
        if (levels > 0) {
            int dirLen = directories.size();
            for (; dirIndex < dirLen; dirIndex++) {
                File current = directories.get(dirIndex);
                File[] children = listFiles(current);
                if (children == null) {
                    logMissingDirectory(current);
                } else {
                    getFiles(levels - 1, children, files, directories);
                }
            }
        }
    }

    private static final Comparator<String> comp =
Collections.reverseOrder(Strings.getNaturalComparator());
    private static File[] listFiles(File p) {
        String[] ss = p.list();
        if (ss == null) {
            return null;
        }
        Arrays.sort(ss, comp);
        int n = ss.length;
        File[] fs = new File[n];
        for (int i = 0; i < n; i++) {
        //private non-validating constructor not accessible (private)
            fs[i] = new File(p, ss[i]);
        }
        return fs;
    }



More information about the nio-discuss mailing list