I want to read all the files in the folder.

Asked 2 years ago, Updated 2 years ago, 45 views

I want to read all the files in the folder in Java, what should I do?

java file io folder

2022-09-22 16:43

1 Answers

public void listFilesForFolder(final File folder) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
        } } else {
            System.out.println(fileEntry.getName());
        }
    }
}

final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);

From java8, it is possible as simple as the code below.

Files.walk(Paths.get("/home/you/Desktop")).forEach(filePath -> {
    if (Files.isRegularFile(filePath)) {
        System.out.println(filePath);
    }
});


2022-09-22 16:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.