• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Simple question about closing files/channels

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Just a simple question:
If I open a RandomAccessFile...

...and then create a FileChannel from that file...

...do I need to call the close method on the RandomAccessFile if I call the close method on the channel first? or vice versa?
i.e. Is it necessary to call close on both objects?
Thanks!
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Hitchens' "Java NIO" book:


An open channel represents a specific connection
to a specific I/O service and encapsulates the state of that connection. When the channel is closed, that connection is lost...


A simple test, do the following:

You'll find that this doesn't run, you get an exception. However, comment out that channel.close() line, and things run fine.
 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very helpful. Thank you.
Does it work the same the other way? i.e. If you close the file, can you still use the channel?
I will test it out...but thought it would be worthwhile to post the answer for those interested.
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't try that, let me know how your test goes.
As far as closing both, I'm not sure if it is good practice to do that, or to let the channel handle it all. Basically, is it definied behavior that a channel will always cleanly close the resource it encapsulates? Something to look in to
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API for RandomAccessFile's close() method:

If this file has an associated channel then the channel is closed as well.


I do not find (in the API) any explicit statement of the converse, that closing a FileChannel will close the associated RandomAccessFile or stream. I believe it's true, but can't quite find a guarantee. Here's something close:

The state of a file channel is intimately connected to that of the object whose getChannel method returned the channel. Changing the channel's position, whether explicitly or by reading or writing bytes, will change the file position of the originating object, and vice versa. Changing the file's length via the file channel will change the length seen via the originating object, and vice versa. Changing the file's content by writing bytes will change the content seen by the originating object, and vice versa.

 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, again, Jim.
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, haven't seen it in the API, only in what I've read in the NIO book and experienced. Again, not sure if this is an expected behavior, but since the closing of the RandomAccessFile actually defines behavior, that would probably be the best way to go
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic