Re: java program that will find a file and words within.
...I accept your challenge/offer "If you would like to code
your own version, I can give
you some tips on where to start."
File.list() and its variant listFiles() and ones that
accept a FileFilter or FilemameFilter are particularly
useful for getting a file list.
If you start with a method that accepts a File, simply
call list() to get a list of files and directories. To
get a list of *all* files in *all* subdirectories, you
might have a method that accepts a File (the root) and
a list such as an ArrayList. Add each file to the list,
and call the same method recursively with the list, for
each directory (File.isDirectory()). By the time the
loops and callbacks are done, you will either end with
a file list, or an OutOfMemoryError!
The File(name)Filter can be used to trim down the
possible search group, based on path, name or extension
(you will need to implement the filters, but they are
In fact, I just realised everything else (searching within
files, for instance) could also be done with a FileFilter,
and the way I think I'd go about it now, is to allow the
user to input regex patterns to search for, and read the
files (that match the other criteria) into a String
to do a String.matches(pattern) on the regex.
You might of course, build all the aspects into a
single 'SearchFileFilter', but there are also reasons
to split them up into more specific filters like:
Message posted via http://www.javakb.com