Results 1 to 4 of 4
  1. #1
    lief480 is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Storing all file names and paths in a directory into an array

    I am trying to create a program that will grab all filenames and their paths and store them into an array. Im having trouble doing this correctly. I am also getting things such as just a folder path. I only want filenames not folder names. Here is the code I have. Any help is appreciated.

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) 
            Scanner keyboard = new Scanner(;
            String path;
            System.out.println("This is a test");
            System.out.println("Enter Directory");
            path = keyboard.nextLine();
            File musicDir = new File(path);
        // Process all files and directories under dir
        public static void visitAllDirsAndFiles(File dir) 
            if (dir.isDirectory()) 
                String[] children = dir.list();
                for (int i=0; i<children.length; i++)
                    visitAllDirsAndFiles(new File(dir, children[i])); 
                    System.out.print("\\" +children[i] + "\n");

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    Well, that code isn't storing anything.
    It's just printing out the contents of a directory, and it's sub directories recursively.

    Is it because it is printing out directories?
    If so that's because there is no check on whether children[i] is a directory.
    In fact, if you simply want the files printed I would say that should be in an else from the if (isDirectory()).

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Why not use listFiles where you get File objects rather than Strings?

    Then you can simply use println getPath (and don't use "\\n" in a print call use println without a "\\n", "\\n" is not what you want on all systems) after checking the type.

    Edit: For God's sake see the File API docs and actually read them forgetting any preconceived notions you have about how you think this should be done.

  4. #4
    wjncross is offline Member
    Join Date
    Oct 2010
    Rep Power


    You are getting file paths printed miced in to the results because you have no check around what the children[I] are. Ever child result makes the method call and prints out irrelevant if it is a directory or file.

    Put a file type check in on the children so that the method is recalled if the child is a directory and prints if it's the file type you want.

    Once you get the results you want the. Replace the print with you array add (check out java Vectors)

Similar Threads

  1. JAR file paths
    By atholon in forum New To Java
    Replies: 6
    Last Post: 05-03-2010, 01:03 AM
  2. Reading a txt file and then storing it in a 2d array
    By blkshp1990 in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 01:31 AM
  3. Storing in an Array
    By Bascotie in forum New To Java
    Replies: 10
    Last Post: 10-15-2009, 05:12 AM
  4. a little help with file paths
    By ashton in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 05:08 PM
  5. [SOLVED] Getting directory paths
    By Leprechaun in forum New To Java
    Replies: 8
    Last Post: 04-23-2008, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts