Java 递归列出目录下的文件以及子目录

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

 
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileFilter;

/**
 * -----------------------------------------------------------------------------
 * This program demonstrates how to list the file or subdirectories in a
 * given directory
 *
 * @version 1.0
 * -----------------------------------------------------------------------------
 */

public class ListFilesSubs {


    /**
     * Used to list the files / subdirectories in a given directory.
     * @param dir Directory to start listing from
     */
    private static void doSimpleFileListing(String dirName) {

        System.out.println();
        System.out.println("Simple file listing...");
        System.out.println("----------------------");

        File dir = new File(dirName);
        
        String[] children = dir.list();

        printFiles(children,dirName);

    }


    /**
     * Used to list the files / subdirectories in a given directory and also
     * provide a filter class.
     * @param dir Directory to start listing from
     * @param ff  A string that can be used to filter out files from the
     *            returned list of files. In this example,the String
     *            values is used to only return those values that start
     *            with the given String.
     */
    private static void doFileFilterListing(String dirName,String ff) {

        System.out.println("Filter file listing...");
        System.out.println("----------------------");

        final String fileFilter = ff;

        File           dir     = new File(dirName);
        FilenameFilter filter  = null;

        if (fileFilter != null) {

            // It is also possible to filter the list of returned files.
            // This example uses the passed in String value (if any) to only
            // list those files that start with the given String.
            filter = new FilenameFilter() {
                public boolean accept(File dir,String name) {
                    return name.startsWith(fileFilter);
                }
            };
        }

        String[] children = dir.list(filter);

        printFiles(children,dirName);

    }


    /**
     * Used to list the files / subdirectories in a given directory and also
     * provide a filter class that only lists the directories.
     * @param dir Directory to start listing from
     */
    private static void doFileFilterDirectoryListing(String dirName) {

        System.out.println("Filter Directory listing...");
        System.out.println("---------------------------");

        File dir = new File(dirName);

        // The list of files can also be retrieved as File objects. In this
        // case,we are just listing all files in the directory. For the sake
        // of this example,we will not print them out here.
        File[] files = (new File(dirName)).listFiles();

        // This filter only returns directories
        FileFilter dirFilter = new FileFilter() {
            public boolean accept(File dir) {
                return dir.isDirectory();
            }
        };

        files = dir.listFiles(dirFilter);

        for (int i=0; i<files.length; i++) {
            System.out.println("[D] : " + files[i]);
        }
        System.out.println();


    }


    /**
     * Utility method to print the list of files to the terminal
     * @param children A String array of the file names to print out
     * @param dirName  The given directory to start the listing at.
     */
    private static void printFiles(String[] children,String dirName) {

        if (children == null) {
            System.out.println("Error with " + dirName);
            System.out.println("Either directory does not exist or is not a directory");
        } else {
            for (int i=0; i<children.length; i++) {
                // Get filename of file or directory
                String filename = children[i];
                if ((new File(dirName + File.separatorChar + filename)).isDirectory()) {
                    System.out.print("[D] : ");
                } else {
                    System.out.print("[F] : ");
                }
                System.out.println(dirName + File.separatorChar + filename);
            }
        }
        System.out.println();

    }

    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     */
    public static void main(String[] args) {

        // Call the simple file listing method
        doSimpleFileListing("new_dir");

        // Now do the file listing but pass in a String to filter the file list
        if (args.length == 0) {
            doFileFilterListing("new_dir",null);
        } else {
            doFileFilterListing("new_dir",args[0]);
        }

        // Now do another example that only prints out the directories
        doFileFilterDirectoryListing("new_dir");

    }
}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...