我想知道爲什麼我會用新的日食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