File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "nature of a BufferedReader  ?" Watch "nature of a BufferedReader  ?" New topic

nature of a BufferedReader ?

jite eghagha
Ranch Hand

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

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 ?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

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

[Jess in Action][AskingGoodQuestions]
Greg Charles

Joined: Oct 01, 2001
Posts: 2961

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:
subject: nature of a BufferedReader ?
It's not a secret anymore!