-2

How would i be able to iterate over a list of different files (in a certain directory) and filter across a List, then if the certain string is found it would return the file it found it in.

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!

Chazmondo
  • 37
  • 1
  • 7
  • Does [**this**](http://stackoverflow.com/questions/3154488/how-do-i-iterate-through-the-files-in-a-directory-in-java) help you? – Aziuth Nov 29 '16 at 14:19
  • @Aziuth not exactly, i wish to also get the contents of the file searching through a List for my certain string and then finally return the file name. – Chazmondo Nov 29 '16 at 14:22
  • Did you write any code? Did you do any searches for the functionality you desire? – VGR Nov 29 '16 at 14:31
  • Yup @VGR, i have found around actually listing my files but i wish to grab each files contents and iterate through the list. – Chazmondo Nov 29 '16 at 14:32
  • It sounds like you already know how to iterate over a list of files, then. And it sounds like your real question is how to search a file for a list of words. Which part of that is giving you difficulty? – VGR Nov 29 '16 at 14:36
  • There is a post for the exact same question here - http://stackoverflow.com/questions/13858966/search-files-by-text-content-inside. – Agrawal S Nov 29 '16 at 14:36

1 Answers1

0

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.