私はすべてを正しく閉じたと思うにもかかわらず、なぜ私は新しいEclipse JUNOでこの警告を受け取るのだろうかと思っています。次のコードでこの警告が表示される理由を教えてください。Eclipse Juno:割り当てられていないcloseable値
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
ストリームを閉じると、ストリームが閉じます。明示的に閉じる必要はありません。 – Strelok
try {}の角括弧を開く前に、新しいFileInputStreamとOutputStreamの宣言が(java7コードの場合)完全に間違っていました。私は彼らにtry-with-resourcesブロックと呼んで言われたと思います。これを訂正した後、警告は消えた。大好きです! –