2

Possible Duplicate:
How to scan a folder in Java?

I want to scan a given folder for all of the files within the folder and add the locations/paths (ex: "c:/users/peter/desktop/image.jpg") to an arraylist of strings. How could i do this? Thanks for the help

Community
  • 1
  • 1
Peter
  • 4,920
  • 22
  • 77
  • 114

3 Answers3

4

Peek around in the java.io.File API. There are at least three methods which may be of use:

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
3

You could try

File directory = new File("<Path to directory>");

String [] directoryContents = directory.list();

List<String> fileLocations = new ArrayList<String>();

for(String fileName: directoryContents) {
    File temp = new File(String.valueOf(directory),fileName);
    fileLocations.add(String.valueOf(temp));
}
MBU
  • 4,858
  • 12
  • 58
  • 97
0

Check out File.list()

Alex Nikolaenkov
  • 2,477
  • 20
  • 27