• 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

close() method; I/O class chaining

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K&B book

Whenever you're done using a file, either reading it or writing
to it, you should invoke the close() method.



But when using chaining with I/O classes is invoking the close() method on the Top wrapped class like PrintWriter or BufferedReader sufficient.
How does the mechanism of close() work when chaining?

 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ram,

in your example you have a PrintWriter wrapping a BufferedWriter wrapping a FileWriter.

Because of this it's sufficient to call close() on the outermost I/O object (PrintWriter) and this one will call close() on the writer it has wrapped which in turn will call close() on the writer it has wrapped and so on until the end of the chain (the innermost writer = FileWriter).

Marco
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark.

This's been a long lasting doubt for me.

Can you point to a link related to this.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately I don't find a really good link at the moment which explains the chaining of Java I/O classes.

Perhaps this Wikipedia article about the decorator pattern may enlighten you. It doesn't describe the Java I/O classes at all, but it's effectively the decorator design pattern which is used when wrapping one object with another as in the case with the Java I/O classes. This and related links hopefully may help you to better understand the concept behind this.

Marco
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic