List all files and folders from the given root. - Java File Path IO

Java examples for File Path IO:Directory Content

Description

List all files and folders from the given root.

Demo Code


import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.swing.filechooser.FileFilter;

public class Main{
    /**/*from  ww w.  j  a va 2  s.c om*/
     * List all files and folders from the given root.
     * 
     * @param root
     *            The root of the listing
     * @return A list of the files under the given root
     */
    public static List<File> listFilesRecursive(final File root) {
        List<File> packedFiles = new ArrayList<File>();

        File[] subFiles = root.listFiles();
        if (subFiles == null) {
            return packedFiles;
        }

        for (File file : subFiles) {
            if (file.isFile()) {
                File packedFile = new File(root, file.getName());
                packedFiles.add(packedFile);
            } else if (file.isDirectory()) {
                packedFiles.addAll(ZipUtils.listFilesRecursive(file));
            }
        }

        return packedFiles;
    }
}

Related Tutorials