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
  • 5,071
  • 24
  • 79
  • 115

3 Answers3

4

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
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,998
  • 12
  • 59
  • 98
0

Check out File.list()

Alex Nikolaenkov
  • 2,505
  • 20
  • 27