我想知道为什么我会用新的日食Juno得到这个警告,尽管我认为我正确地关闭了所有东西。你能告诉我为什么我在下面这段代码中得到这个警告吗?Eclipse Juno:未分配的可关闭值
public static boolean copyFile(String fileSource, String fileDestination)
{
try
{
// Create channel on the source (the line below generates a warning unassigned closeable value)
FileChannel srcChannel = new FileInputStream(fileSource).getChannel();
// Create channel on the destination (the line below generates a warning unassigned closeable value)
FileChannel dstChannel = new FileOutputStream(fileDestination).getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
return true;
}
catch (IOException e)
{
return false;
}
}
这个工程,但我想现在如何删除这个警告,而不使用此功能!为什么不能直接在资源中声明FileChannel。编辑:你只是回答我的问题,但为什么你不关闭fileChannel? – Abbadon 2012-08-07 07:45:50
当你关闭流时,它会关闭通道。你不需要明确地关闭它。 – Strelok 2012-08-07 07:54:33
我完全错过了(对于java7代码)新的FileInputStream和OutputStream的声明发生在打开try {}的括号之前。我想你提到过,通过调用它们来尝试与资源块。纠正后,警告消失。爱它! – 2017-11-20 12:03:00