Search code examples
androidtry-catchsuppress-warnings

Android API level < 19 and "try can use automatic resource management" warning


I have this piece of code

private void copyFile(File src, File dst) throws IOException {

    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();

    try {

        inChannel.transferTo(0, inChannel.size(), outChannel);

    }finally {

        if(inChannel != null) {

            inChannel.close();

        }

        outChannel.close();

    }

}

that generates the annoying warning "try can use automatic resource management". The prolem is I cannot use Java 7 try-with-resources cause my app targets api level 14 (got an error if I use it). Is there a way to suppress that annoying warning?


Solution

  • @SuppressWarnings("TryFinallyCanBeTryWithResources") did the trick :)