• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileReader.read() isn't returning -1 at the end-of-stream  RSS feed

 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to read a file and print it's content character by character until the end. To do that I am using the following snippet,



"input.txt" file contains "12345" no carriage return or line feed at the end. The method does print all the characters from the file, but at the end it keeps on printing unknown characters - like the following.

"12345￿
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Seems like I can't update my original post)

Question:

I know that f.read() would return -1 at the end-of-stream which should stop the loop, but why that's not happening here?
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The read() method returns an int, which you cast to a char and then cast back to an int so you can compare it to -1. So your -1 is getting changed to something else in that process. I would recommend moving the cast to char somewhere else, so it doesn't mess up your end-of-file testing.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul is correct; the problem is that chars are unsigned so they never support the value −1.

Why are you using the read() method? Unless it is to find out how bad that method is, you should avoid read() (and write()). Wrap that reader in a buffered reader and read the whole line. Or (probably easier) use a Scanner. ReadAllAboutIt (as the people who stood in the middle of the road selling newspapers said when I was little) in the Java™ Tutorials.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. That's was the problem. The solved code,



I am trying write a lexical analyzer, and I need to read from the input file character by character to see what token is it. That's why I want to avoid reading line. I can of course read lines and parse it character by character...

But what's wrong with read() and write()? Should I both avoid the them from both byte/character class?
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are the successive tokens delimited? For a parser, are you parsing code? Do you allow constructs like
x+y
or do you require the tokens be spaced apart: x + y? You would find it awkward to read x+y as three tokens with a Scanner.
I think you would do better to read a whole line and scan the line because a buffered reader/Scanner takes only slightly longer to read a whole line than read() does to read one letter.You would have identifyWord() methods and identifyNumber(), identifyOperator(), etc etc.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:But what's wrong with read() and write()?

Nothing, they're just rather laborious.

And if you're file is arranged in lines, doesn't it make sense to read it in that way? Then you can determine "tokens" by whatever the rules are for them.

Furthermore, assuming tokens can't be split across lines, you can actually use a Stream to aid the process in version 8, viz:
HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on there being a token delimiter. If x+y and x + y are indistinguishable, then it would be very awkward to split on a delimiter.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:. . . But what's wrong with read() and write()? . . .
I thought you had found that out already. They are slow. The read() method returns an int when you want a char. They declare all sorts of checked Exceptions which never actually are thrown.

But the most important objection to them is that there are better alternatives available.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That depends on there being a token delimiter. If x+y and x + y are indistinguishable, then it would be very awkward to split on a delimiter.

True. As it is, it would also rely on the file contents being correct.

It seems to me that File-to-Stream functions are still a bit thin on the ground. Maybe we'll get some more with v9.

Winston
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . As it is, it would also rely on the file contents being correct. . . .
At least if you parse a file's contents you can check them for correctness. It is much harder to check the other files people read from.
I think the Stream methods didn't take parsing and lexing into consideration because those are specialised techniques. Also there are libraries like JFlex already available.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!