简体   繁体   中英

Save all files recursively in a List

I have a method which takes a String parameter as the root directory and I want to save for all subfolder the names and path for the files only.

FileObject is just a class with two String variables - name and path .

private static List<FileObject> getAllFilePaths(String rootDir, List<FileObject> helper) {
    File directory = new File(rootDir);
    System.out.println("rootDir: "+rootDir);
    FileObject fo=new FileObject();
    //get all files and directories
    File[] allFiles = directory.listFiles();
    for (File file : allFiles) {
        if (file.isFile()) {
            System.out.println("If file: "+file.getName());
            //if it's a file add it to results
            fo.setPath(file.getAbsolutePath());
            fo.setName(file.getName());
            helper.add(fo);
        } else if (file.isDirectory()) {
            System.out.println("else if: "+file.getName());
            //is it's a directory do the method again and add to to results
            //fo.setPath(file.getAbsolutePath());
            //fo.setName(file.getName());
            //results.add(fo);
            getAllFilePaths(file.getAbsolutePath(),helper);
        }
    }
    return helper;
}

I tried many different way to implement this method but the returned List is wrong. How do I do this correctly?

You're only creating one instance of a FileObject , and just adding it to the list over and over again. Despite using the setter methods (like setName() and setPath() ) to modify its properties, this is not going to give you the results you're expecting.

Java does not copy an object when it adds it to a collection/data structure. So by calling the set...() methods in a for loop, you're altering the existing instance that you've already added to the list.

Try moving the instantiation of your FileObject into your loop:

private static List<FileObject> getAllFilePaths(String rootDir, List<FileObject> helper) {
    File directory = new File(rootDir);
    System.out.println("rootDir: "+rootDir);
    //get all files and directories
    File[] allFiles = directory.listFiles();
    for (File file : allFiles) {
        if (file.isFile()) {
            System.out.println("If file: "+file.getName());
            //if it's a file add it to results

            FileObject fo=new FileObject(); // < Instantiate the FileObject here

            fo.setPath(file.getAbsolutePath());
            fo.setName(file.getName());
            helper.add(fo);
        } else if (file.isDirectory()) {
            System.out.println("else if: "+file.getName());
            //is it's a directory do the method again and add to to results
            //fo.setPath(file.getAbsolutePath());
            //fo.setName(file.getName());
            //results.add(fo);
            getAllFilePaths(file.getAbsolutePath(),helper);
        }
    }
    return helper;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM