File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes nature of a BufferedReader  ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
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: 127
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: 24187
    
  34

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


[Jess in Action][AskingGoodQuestions]
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2853
    
  11

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://aspose.com/file-tools
 
subject: nature of a BufferedReader ?