This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Simple question about closing files/channels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Simple question about closing files/channels" Watch "Simple question about closing files/channels" New topic
Author

Simple question about closing files/channels

Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
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!


Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
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

Joined: Jan 07, 2002
Posts: 89
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

Joined: May 08, 2001
Posts: 1932
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
Thanks, again, Jim.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple question about closing files/channels