티스토리 뷰

Use "java.nio.Files#delete" here for better messages on error conditions.

SonarQube를 이용하니 새로운 기술도 알려 주네요. ^^

수정 전

public static boolean deleteFile(String path, String name){

     File file = new File(path + name);

if(file.exists()){

return file.delete();

}

return false;

}

수정 후 

    public static boolean deleteFile(String path, String name) {

        Path filePath = FileSystems.getDefault().getPath(path, name);

        try {

            Files.delete(filePath);

        } catch (IOException | SecurityException e) {

            return false;

        }

        return true;

    }


"java.nio.Files#delete" should be preferred (squid:S4042)

CODE_SMELL Code smell MAJOR Major

When java.io.File#delete fails, this boolean method simply returns false with no indication of the cause. On the other hand, when java.nio.Files#delete fails, this void method returns one of a series of exception types to better indicate the cause of the failure. And since more information is generally better in a debugging situation, java.nio.Files#delete is the preferred option.

Noncompliant Code Example

public void cleanUp(Path path) {
  File file = new File(path);
  if (!file.delete()) {  // Noncompliant
    //...
  }
}

Compliant Solution

public void cleanUp(Path path) throws NoSuchFileException, DirectoryNotEmptyException, IOException{
  Files.delete(path);
}


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함