How to scan a folder for files and their locations in java [duplicate]
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
Peek around in the java.io.File
API. There are at least three methods which may be of use:
listFiles()
isDirectory()
isFile()
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));
}
Check out File.list()
精彩评论