This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

String Searching in huge Text file

Hs Raveendran
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David O'Meara
Rancher

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.
 
Consider Paul's rocket mass heater.
 
subject: String Searching in huge Text file