get all files in a folder java code example

Example 1: how to get all the names of the files in a folder in java?

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


File[] files = new File("/path/to/the/directory").listFiles();
//If this pathname does not denote a directory, then listFiles() returns null. 

for (File file : files) {
    if (file.isFile()) {
        results.add(file.getName());
    }
}

Example 2: java get folder content

File file  = new File("Path to file");
File[] files = file.listFiles();

Tags:

Java Example