aspose file tools
The moose likes Java in General and the fly likes nature of a BufferedReader  ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "nature of a BufferedReader  ?" Watch "nature of a BufferedReader  ?" New topic
Author

nature of a BufferedReader ?

jite eghagha
Ranch Hand

Joined: Oct 06, 2006
Posts: 124
StringTokenizer tokens = new StringTokenizer(stringToTokenize);
while(tokens.hasMoreTokens()) {
String s = tokens.nextToken();
while ((wordToExclud = in.readLine()) != null) {
if(s.equalsIgnoreCase(wordToExclud))
processedString += s + " ";
}
}
in.close();

If "in" is a bufferedReader, containing 100 lines of a text file, what happens within the innner while loop after line 100 ?

Will "in" go to line 1, on the outter whiles second iteration ?

eghagha
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

No. You get to the end, and then readLine() returns null. It never "rewinds".


[Jess in Action][AskingGoodQuestions]
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2550
    
  10

You could overcome your dilemma by reading all the lines into a data structure, and then using that to compare to you string tokens. Think very carefully about the amount of work you are doing though. If there are 50 tokens and 100 lines, then you will be doing 5000 string comparisons. You could reduce that number by using a binary search, or at least by breaking out of the inner loop once a match was found.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: nature of a BufferedReader ?
 
Similar Threads
BufferedWriter->readLine()
testing
count how many tokens are in the first line
reading csv file
testing a stringtokenizer