Charlie J Charlie J - 1 year ago 74
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

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:
- football
- hockey
- custom

if custom is found, the first match it finds to return the file name, eg


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download