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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 19543
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Searching in huge Text file
 
Similar Threads
JTextPane.setPage() is so slow when the target page is huge
Problem with reading a huge text file efficiently
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?
Calling awk script from java programs