Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes read() of FileReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "read() of FileReader" Watch "read() of FileReader" New topic
Author

read() of FileReader

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96

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


Constant dripping hollows out a stone....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
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.


Ping & DNS - my free Android networking tools app
Jim Jonides
Ranch Hand

Joined: Oct 12, 2010
Posts: 42
Totally agree with Ulf. It will become obvious if you change the file reading code to following:


Devil is in the detail.
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Thanks Ulf.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: read() of FileReader