• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Searching in huge Text file  RSS feed

 
Raveendran Hs
Greenhorn
Posts: 23
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20898
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!