wood burning stoves 2.0*
The moose likes Java in General and the fly likes String Searching in huge Text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Searching in huge Text file" Watch "String Searching in huge Text file" New topic

String Searching in huge Text file

Hs Raveendran

Joined: Feb 05, 2010
Posts: 23

What is the best and efficient way to count the number of times the given keyword appeared in the huge text file.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19653

Unless the word can be broken over two lines (using the - character to break), simply read each line one by one (using BufferedReader perhaps), then use indexOf. Unless you want a case insensitive search, then perhaps a java.util.regex.Pattern (declared outside the loop) with the Pattern.CASE_INSENSITIVE flag set and a Matcher inside the loop can be used. In pseudo code:
Now, if the word can be broken over two lines, that's going to be tricky; you'll require multiple lines in memory at the same time; at least two.

How To Ask Questions How To Answer Questions
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

does it need to be in Java? If it is just a one-off task I tend to use Unix command line tools for tasks like this.
jQuery in Action, 2nd edition
subject: String Searching in huge Text file
Similar Threads
JTextPane.setPage() is so slow when the target page is huge
Calling awk script from java programs
Suggest a best way to read, process and write text file to file system. Huge text file in few MBs
how many ways do two java program communicate?
Problem with reading a huge text file efficiently