• Post Reply Bookmark Topic Watch Topic
  • New Topic

StreamTokenizer vs readLine

 
ray gchang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a grogram to read a text file in.
for a line, read the first word in using StreamTokenizer and then the second char(a ":") and then using readLine to read the rest of the line in.
the following loop is surposed to do this and the 1st iteration works well but then the StreamTokenizer doesn't work and the readLine read all line in.
Can anybody tell me what is the problem?
Thank you very much.
 
ray gchang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code is here.
for (int i=0; i<surCount; i++) {
tokenType=inputStream.nextToken();//read the first work
arrPrefix[i]=inputStream.sval;
//read the 2nd character
tokenType=inputStream.nextToken();
//read the rest of the line
arrName[i]=buffer.readLine();
}
My question is can I use StreamTokenizer and readLine together? How can I use them together to avoid this kind of thing?
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A BufferedReader buffers its input (hence the name). That means that it looks ahead when it can. By looking ahead, it's stealing information that you were expecting the StreamTokenizer to read.
Rule number one: trying to access the same underlying stream via two seperate adaptors can only lead to disaster.
Rule number two: If you're considering using StreamTokenizer -- don't. It's an evil, evil class with very little to recommend it. Poor interface, performance, documentation, etc.
The solution: add 29 characters to the program and parse the first token yourself . It sounds like you can just read the entire line (first word and all) and then cut it in half at the first occurance of ':'.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!