Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question about closing files/channels

 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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!
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • 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
  • 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 ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks, again, Jim.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic