• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read() of FileReader

 
Deepak Bobal
Ranch Hand
Posts: 96
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output
File is written
Data is 84
104-- T
105-- h
115-- i
32-- s
105--
115-- i
32-- s
84--
101-- T
120-- e
116-- x
32-- t
102--
105-- f
108-- i
101-- l
-1-- e


read() of FileReader class , returns int value but at line 3 it seems that it returns array of integers as i apply while loop on it.I dont understand output "Data is 84 " from line 2 . how this 84 is coming.

Thank You
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read() returns an int, as its javadocs specify. Line "2" prints that integer, and the javadocs explain what that number means.

You are not applying the while loop to what a single read() returns, you are repeatedly calling read() and it returns a different number each time.

The real problem that I think is hindering your understanding is that lines 21 and 22 are switched around - the output does not line up the characters with their respective numerical values correctly.


} catch (IOException i) {
}

You should never ignore IO exceptions; at the very least write a message to the console so that you'll know if something went wrong.

Also, you've been around the ranch long enough to know that you should UseCodeTags when posting code. It's unnecessarily hard to read it otherwise, making it less likely that people will bother to do so.
 
Jim Jonides
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Totally agree with Ulf. It will become obvious if you change the file reading code to following:

 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic