Skip to content
Advertisement

Search for all po files in a directory and its subdirectories and create a Hashmap containing the filepaths as value and its directory as key

I want to search for all .po files in a directory and add them to a HashMap.

My directories looks like the following:

JavaScript

How can I represent/search these files with the help of java-Streams and HashMap<String, List<File>>, with en/de/it.po as a List<File> type and the respective directory root of these files dir1subdir-1**subdir-n as a key?

I’ve tried the following solution from a similar post: https://stackoverflow.com/a/62824848/10042081 but I wasn’t able to manipulate the filter/groupings correctly in order to solve my problem.
I’ve also tried to use listFiles() + isDirectory() recursively in order to achieve the desired result, with no success.

Advertisement

Answer

This is pretty simple using Files.find() to walk a directory tree looking for matching files, and Collectors.groupingBy() to convert that stream into the desired map.

JavaScript

This will give you a map where the keys are paths to directories, and the values are lists of paths to all the .po files in those directories.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement