Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileFilter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Get files of directory you passed. The files contains files of passed directory's sub directory by recursive exploring.
     */
    public static List<File> getAllFiles(File directory) {
        final List<File> fileList = new LinkedList<File>();
        final File[] files = directory.listFiles();

        if (files == null)
            return fileList;
        for (File file : files) {
            if (file.isDirectory())
                fileList.addAll(getAllFiles(file));
            else
                fileList.add(file);
        }
        return fileList;
    }

    /**
     * @see #getAllFiles(File)
     */
    public static List<File> getAllFiles(File directory, FileFilter filter) {
        final List<File> fileList = getAllFiles(directory);
        if (filter == null)
            return fileList;

        final Iterator<File> fileIterator = fileList.iterator();
        while (fileIterator.hasNext()) {
            final File file = fileIterator.next();
            if (!filter.accept(file))
                fileList.remove(file);
        }
        return fileList;
    }
}