0

I am attempting to open a folder and list it's contents (files). Basically I have a top folder containing several project folders and within those project folders are csv's and png's. I want to be able to crack through those two folders and list the contents, then once that is complete, be able to go back out and enter the next project folder and do the same thing. So far I am able to list all the files within a specified folder. This is what I have:

import java.io.*;

public class testtwo {

public static void main(String[] args) {

    testtwo directory = new testtwo();
    directory.showFileList();
    }

private void showFileList() {
    File directory = new File("CAD_Import");
    File[] filesInsideDirectory = directory.listFiles();
    for(File file : filesInsideDirectory) {
        System.out.println("File Name : " + file.getName());
    }
  }
}

Thanks!

jmpman

jmpman
  • 23
  • 1
  • 13
  • possible duplicate of [List all files from a directory recursively with Java](http://stackoverflow.com/questions/2534632/list-all-files-from-a-directory-recursively-with-java) – Kon Jan 12 '15 at 16:01
  • Sort of like the dir command on cmd.exe? – CaffeineToCode Jan 12 '15 at 16:06

1 Answers1

0

It's a classic recursion question, I suggest you have a look at this first.

Also a piece of code to help you get started.

private void printContents(File directory){
  for(File f : directory.listFiles()){
   System.out.println(f);
   if(f.isDirectory())
    printContents(f);
  }
}
Community
  • 1
  • 1
Muli Yulzary
  • 2,559
  • 3
  • 21
  • 39