0

I have a root folder and a bunch of files and folders within. I want to generate a list of file names that are within that root folder (and sub folders), along with the location of each file.

I need the file location because I want to display the list of file names to the user and let him open the file if needed.

I have used the following code to get the file names but I'm lost on how to get the matching file locations.

public void listFilesForFolder(final File folder) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
        } else {
            System.out.println(fileEntry.getName());
        }
    }
}

final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);

Also, right now, I'm using an array list to store the file names. Any better way/structure to make this work would also be helpful.

john
  • 1,561
  • 3
  • 20
  • 44
  • 2
    Are you looking for `fileEntry.getPath()`? – aioobe Aug 19 '14 at 05:40
  • 2
    possible duplicate of [Recursively list files in Java](http://stackoverflow.com/questions/2056221/recursively-list-files-in-java) – BetaRide Aug 19 '14 at 05:42
  • @aioobe will this search for the file using the file name? If so, that would be slow so that's not what I need. If not, could you please link to a resource on this? – john Aug 19 '14 at 05:42
  • @BetaRide Thanks. Couldn't find that before. – john Aug 19 '14 at 05:52

3 Answers3

1

This is what I used. Works perfectly.

  import java.io.File;

        public class Filewalker {

            public void walk( String path ) {

                File root = new File( path );
                File[] list = root.listFiles();

                if (list == null) return;

                for ( File f : list ) {
                    if ( f.isDirectory() ) {
                        walk( f.getAbsolutePath() );
                        System.out.println( "Dir:" + f.getAbsoluteFile() );
                    }
                    else {
                        System.out.println( "File:" + f.getAbsoluteFile() );
                    }
                }
            }

            public static void main(String[] args) {
                Filewalker fw = new Filewalker();
                fw.walk("c:\\" );
            }
        }
john
  • 1,561
  • 3
  • 20
  • 44
0

You can use some Tree data structure. Its a perfect suite for your requirement.

File is a leaf node of the tree. Folder is the parent of the tree. Leaf does nt have child.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
0

use linkedhashmap store the name and path pair.

ben
  • 71
  • 2
  • could you explain how your answer helps the OP? – Kick Buttowski Aug 19 '14 at 06:18
  • 1
    "Also, right now, I'm using an array list to store the file names. Any better way/structure to make this work would also be helpful." as he said.if he want store name and path pair. linkedhashmap is good structure for iterating and search. – ben Aug 19 '14 at 06:40
  • your answer is right, yet it needs more info just a friendly advice :) – Kick Buttowski Aug 19 '14 at 06:54