It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K*B 1.5 book . PG 433 Error 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 "K*B 1.5 book . PG 433 Error" Watch "K*B 1.5 book . PG 433 Error" New topic

K*B 1.5 book . PG 433 Error

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
There is a code in K&B that shows how to use FileWriter and FileReader to write and read the contents of a file.

The output :
Boxes till the end....... [Square boxes]

As the read(char []dest) method reads the number of characters that is equal to the size of the declared array dest.
In the above program since of size of array is far greater than the length of the file , Using for(char c est) { } will iterate to the length of the array instead of the lenght of the file. Hence the above code should be modified to iterate the dest[] till the size returned by the read(). Hence the code should be modified as follows.

This will now display the excat contents of the file.
This is not mentioned in the K&B errata in the site.
Hope am correct.

Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Excellent catch. If I'm not mistaken, most systems will render the remaining zero-value bytes in the array as spaces, so there probably will not be a visible difference to the casual observer. But your correction is absolutely right.

SCJP 5.0
I agree. Here's the link:
subject: K*B 1.5 book . PG 433 Error
It's not a secret anymore!