Charlie J Charlie J - 9 days ago 5
Java Question

Iterating over files in directory to find content

How could i iterate over a list of files (within a directory) iterating over a

List<String>
and if a certain string is found within all files to return the file name.

Eg: Files are '344-444.yml, 555-646.yml and 996-666.yml
File 344-444.yml contains:
hobby:
- football
- hockey
- custom


if custom is found, the first match it finds to return the file name, eg
344-444.yml
.

Thanks!

Answer

Your explanation of the objective is a bit unclear. However, I understood that at a certain point you will have your list of strings (the dictionary) and you have to seach each string on a specific file. A good algorithm for this is the Aho-Corasick algorithm which will give you complexity linear in the length of the file + number of occurences + length of dictionary. If the size of your dictionary is large, this is a huge time gain with respect to looking for one string at a time.