Filter all files with a particular extension in Java

628


How can i filter files with particular extension in Java, let say i want to filter all .json files from a folder.

1 Answer

6 years ago by

Following code filters all json files from the flder /path/to/folder

Arrays.asList(Paths.get("/path/to/folder").toFile().listFiles(file -> !file.isHidden() &&  file.isFile() && (file.getName().endsWith(".json")))).forEach(file -> {
	// deal with the file
});
6 years ago by Karthik Divi