My task is to write a class RFind which recursively searches the current directory and its subdirectories, attempting to match files against its single regular expression argument.
So far the only thing I have tried is the following which a) isn’t recursive and b) doesn’t really seem to work. How would you go about doing this?
NB: The sample solution given displays how you could pass regex in the command line and how it would look:
$ java RFind “.*.java”
./RFind.java
./src/AHTNF.java
$
public class RFind {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter a regular expression: ");
String regex = in.next();
Pattern p = Pattern.compile(regex);
File dir = new File (".");
File[] files = dir.listFiles();
ArrayList<String> f = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
Matcher m = p.matcher(files[i].toString());
if (m.matches()) {
f.add(files[i].toString());
}
}
for (int i = 0; i < f.size(); i++) {
System.out.println(f.get(i));
}
}
}
To be recursive you need at least one method to be called
May run, not compiled yet