에 나는 코드FileInputStream에 오류가 SonarQube
문제는 다음 날은 API LEVEL 19+
위입니다 try-with-resources
를 사용하도록 제안 sonarqube을 SonarQube
중요한 문제가 무엇입니까 그리고 난 minimumSDK
로 LEVEL 16
을 목표로하고있다.
어떤 식 으로든 아래 코드에서 이미 완료 한 finally
블록에있는 FileInputStream
블록을 보여주는 것입니다.
protected void copyFile(File sourceFile, File destFile) throws IOException {
if (!sourceFile.exists()) {
return;
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (source != null) {
destination.transferFrom(source, 0, source.size());
}
} finally {
if (source != null && destination != null) {
source.close();
destination.close();
}
}
}
심지어 안드로이드 스튜디오 빨간색 줄 오류를 제공하고 동일 finally
블록에 try-with-resources
또는 가까운 스트림을 사용하도록 제안합니다.
UPDATE 모두가 null 인 경우에만 두 채널을 폐쇄하고, 당신이 스트림을 폐쇄하지 않는 때문입니다
시도해 보았지만 finally 블록에서 닫는 스트림을 나타내는 오류가 여전히 발생했습니다. – Aks4125
죄송합니다. close()가 Exception을 throw 할 수 있기 때문입니다. 나는 대답을 업데이트 할 것이다. – fejd
업데이트 된 질문을 확인하십시오. 업데이트 된 솔루션을 사용했습니다. – Aks4125