File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 19672

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.
It is sorta covered in the JavaRanch Style Guide.
subject: String Searching in huge Text file