Android API级别<19和"尝试可以使用自动资源管理"警告

Sta*_*ego 15 android try-catch suppress-warnings

我有这段代码

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();

    }

}
Run Code Online (Sandbox Code Playgroud)

产生恼人的警告"尝试可以使用自动资源管理".问题是我不能使用Java 7 try-with-resources导致我的应用程序目标api级别14(如果我使用它会出错).有没有办法压制那令人讨厌的警告?

Sta*_*ego 27

@SuppressWarnings("TryFinallyCanBeTryWithResources")做了诀窍:)

  • 您可以执行"分析/检查代码",然后关闭对话框中语句的检查.从AS2.2RC开始,我得到了这个:`// noinspection TryFinallyCanBeTryWithResources`在`try`行之上添加.同意当最低级别设置为16时获得此Lint警告非常烦人,并且该功能仅在19时可用! (3认同)