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;

public class Main {
    /**
     * Returns count of files contained recursively in the specified directory.
     * If the dir does not exist then it returns 0.
     */
    public static int count(File dir, FileFilter fileFilter, boolean includeFolders) {
        int result = 0;
        if (!dir.exists()) {
            return 0;
        }
        for (final File file : dir.listFiles()) {
            if (file.isFile() && !fileFilter.accept(file)) {
                continue;
            }
            if (file.isDirectory()) {
                if (includeFolders) {
                    result++;
                }
                result = result + count(file, fileFilter, includeFolders);
            } else {
                result++;
            }
        }
        return result;
    }
}