开发者

How to scan a folder for files and their locations in java [duplicate]

This question already has answers here: Closed 10 years ago.

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()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜