• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Closing resources

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

In the line BufferedReader br = new BufferedReader(new Filereader("myFile.txt")), I see two resources, one is br, and one is anonymous.

Now try-with-resources closes br, but what about the anonymous resource? Do inner resources get closed when the outer resource gets closed?

Thanks.
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Smithonian wrote:Do inner resources get closed when the outer resource gets closed?


Although the documentation isn't very clear on it (it just mentions freeing all resources), BufferedReader closes the Reader it wraps. The same goes for BufferedInputStream, FilterReader and FilterInputStream. The matching output classes (BufferedWriter, BufferedOutputStream, FilterWriter and FilterOutputStream) do so as well.
 
Marshal
Posts: 76447
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find out about try with resources, which is probably the best way to ensure resources are closed.

Never never close anything pointing to System.in or System.out or System.err.
 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, thanks guys.
 
Campbell Ritchie
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Saloon Keeper
Posts: 14300
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing, you technically only need to close inner resources. If you have multiple wrappers around a single resource, you only have to close the inner one:



Be wary that you shouldn't do this with output streams that have cleanup operations (such as flushing a buffer or finishing a compression or encryption algorithm).
 
Attractive, successful people love this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic