티스토리 뷰
Use "java.nio.Files#delete" here for better messages on error conditions.
수정 전
public static boolean deleteFile(String path, String name){
File file = new File(path + name);
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)
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);
}
'리팩토링(스프링기반)' 카테고리의 다른 글
| Remove this "BigDecimal" constructor (0) | 2019.01.08 |
|---|---|
| private 생성자 추가 (0) | 2019.01.08 |
| 동일 처리를 하는 catch문 합치기 (0) | 2019.01.08 |
| private method의 @Transactional 제거 (0) | 2019.01.08 |
| Map 전체 내용을 읽을 때 Key, Value를 모두 사용한다면 "entrySet"을 사용하라 (0) | 2019.01.08 |
- Total
- Today
- Yesterday
- mybatis
- 예제
- 메시지
- RESTful
- 그리드
- mapToList
- 타임리프
- Javascript
- AG-GRID
- Spring Boot
- 샘플
- lombok
- restful서비스
- listToMap
- thymeleaf
- 스프링부트
- spring
- 설정
- UI
- springboot
- 스프링
- ag grid
- oracle
- example
- SHEETJS
- REST
- sample
- 엑셀
- java
- cache
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |