• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K*B 1.5 book . PG 433 Error

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
abc
def
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.
Thanks
Deepak
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This one time, at bandcamp, I had relations with a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic