File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nature of a BufferedReader ?

 
jite eghagha
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You get to the end, and then readLine() returns null. It never "rewinds".
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic