I am working on a project where I am recursing through a directory and checking all files/subdirectory files for a particular string. If the file contains the string I need to remove it.

Currently the way I handle this situation is by using Scanner on each file, taking the file in Line by Line and checking if the line.contains("mystring"). If it does, I remove it, and write the file, etc.

This seems to be very inefficent if a file does not contain mystring, as we still going through the file line by line.

With that being said, does anyone have any brighter ways to determine whether the file contains my string without having to parse through the file line by line to determine that?

Thanks.

02-20-2010, 04:27 AM

collin389

Well, if you read the file into a String, you could then use indexOf("mystring") and that would tell you if the file contained that word, but you still have to read the file into a string and indexOf still looks at every word, you just wouldnt have to have a big for loop.

02-20-2010, 04:42 AM

Moncleared

Yea that doesn't really sound like it would cut down on the efficency. How can I read an entire file into a single string curiously? Or what is the easiest way...

02-20-2010, 05:11 AM

collin389

Yeah, you would still have to read it line by line, or, to be more efficient if you could use a buffer so you don't use too much memory.