Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Juno : unassigned closeable value

Tags:

java

eclipse

I am wondering why I get this warning with the new eclipse Juno despite I think I correctly closed everything. Could you please tell me why I get this warning in the following piece of code?

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;
    } 
 }
like image 367
Abbadon Avatar asked Aug 07 '12 07:08

Abbadon


3 Answers

IF you're running on Java 7, you can use the new try-with-resources blocks like so, and your streams will be automatically closed:

public static boolean copyFile(String fileSource, String fileDestination)
{
    try(
      FileInputStream srcStream = new FileInputStream(fileSource); 
      FileOutputStream dstStream = new FileOutputStream(fileDestination) )
    {
        dstStream.getChannel().transferFrom(srcStream.getChannel(), 0, srcStream.getChannel().size());
        return true;
    }
    catch (IOException e)
    {
        return false;
    } 
}

You won't need to explicitly close the underlying channels. However if you're not using Java 7, you should write the code in a cumbersome old way, with finally blocks:

public static boolean copyFile(String fileSource, String fileDestination)
{
    FileInputStream srcStream=null;
    FileOutputStream dstStream=null;
    try {
      srcStream = new FileInputStream(fileSource); 
      dstStream = new FileOutputStream(fileDestination)
      dstStream.getChannel().transferFrom(srcStream.getChannel(), 0, srcStream.getChannel().size());
        return true;
    }
    catch (IOException e)
    {
        return false;
    } finally {
      try { srcStream.close(); } catch (Exception e) {}
      try { dstStream.close(); } catch (Exception e) {}
    }
}

See how much better the Java 7 version is :)

like image 136
Strelok Avatar answered Nov 11 '22 04:11

Strelok


You should always close in finally because if an exception rise, you won't close the resources.

FileChannel srcChannel = null
try {
   srcChannel = xxx;
} finally {
  if (srcChannel != null) {
    srcChannel.close();
  }
}

Note: even if you put a return in the catch block, the finally block will be done.

like image 4
Michael Laffargue Avatar answered Nov 11 '22 04:11

Michael Laffargue


eclipse is warning you about the FileInputStream and FileOutputStream that you can no longer reference.

like image 3
VolatileDream Avatar answered Nov 11 '22 04:11

VolatileDream