aspose file tools
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 OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP 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: 19847

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