package listem; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GrepImpl extends superClass implements Grep { public GrepImpl(){ } public Map> grep(File directory, String fileSelectionPattern, String substringSelectionPattern, boolean recursive){ Map> mymap = new HashMap>(); ArrayList myfiles = new ArrayList(); myfiles = super.initialsearch(directory, fileSelectionPattern, recursive); for(int i = 0; i < myfiles.size(); i++){ List word = process(myfiles.get(i), substringSelectionPattern); if(word.size() > 0){ mymap.put(myfiles.get(i), word); } } return mymap; } public List process(File temp, String substringSelection){ List word = new ArrayList(); Pattern p = Pattern.compile(substringSelection); try { Scanner scanner = new Scanner(temp); while(scanner.hasNextLine()){ String line = scanner.nextLine(); Matcher m = p.matcher(line); if(m.find()){ word.add(line); } } } catch (FileNotFoundException e) { e.printStackTrace(); } return word; } }