Like the title; Does closing a FileChannel
close the underlying file stream?
From the AbstractInterruptibleChannel.close()
API docs you can read:
Closes this channel.
If the channel has already been closed then this method returns immediately. Otherwise it marks the channel as closed and then invokes the
implCloseChannel
method in order to complete the close operation.
Which invokes AbstractInterruptibleChannel.implCloseChannel
:
Closes this channel.
This method is invoked by the close method in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once.
An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.
And that doesn't say anything about the stream. So in fact, when I do:
public static void copyFile(File from, File to)
throws IOException, FileNotFoundException {
FileChannel sc = null;
FileChannel dc = null;
try {
to.createNewFile();
sc = new FileInputStream(from).getChannel();
dc = new FileOutputStream(to).getChannel();
long pos = 0;
long total = sc.size();
while (pos < total)
pos += dc.transferFrom(sc, pos, total - pos);
} finally {
if (sc != null)
sc.close();
if (dc != null)
dc.close();
}
}
...I leave the streams open?