jQuery in Action, 3rd edition
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

read() of FileReader

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96

File is written
Data is 84
104-- T
105-- h
115-- i
32-- s
115-- i
32-- s
101-- T
120-- e
116-- x
32-- t
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

Joined: Mar 22, 2005
Posts: 42965
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

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.
I agree. Here's the link: http://aspose.com/file-tools
subject: read() of FileReader
jQuery in Action, 3rd edition