728x90

1. 파일 삭제

File file = new File("C:/Temp/abc.jpg");

if(file.exists()) { //파일이 존재한다면
	file.delete(); // 삭제
	System.out.println(fileDTO.getStoredFileName() + " : 삭제 완료");
}
else {
	System.out.println("삭제할 파일이 없습니다.");
}
  • File 객체를 생성할 때 생성자에 존재하는 파일을 지정
  • File.delete()를 사용해서 삭제 가능
  • File.exists() : 파일의 존재 여부
728x90

Spring Boot프로젝트 중 게시글에 첨부된 이미지 데이터를 DAO를 통해 List로 반환하려한다.

이 때 반환받은 List가 Null인지 체크할 때 사용

  


1. Spring을 사용하는 경우 (CollectionUtils.isEmpty)

List<FileDTO> files = fileService.getFileList(boardId);

CollectionUtils.isEmpty(files)
  • FileDTO(이미지 파일 정보)를 DB에서 게시판 아이디로 조회하여 받아오는 코드이다.
  • CollectionUtils.isEmpty()를 이용해서 List가 null인지 체크 가능

2. Java

List<String> list = new ArrayList<>();

list.isEmpty();
  • isEmpty()를 이용해서 List가 null인지 체크 가능

 

* 참고 : https://jihyehwang09.github.io/2020/04/13/java-list-null-check/

 

Java- List의 Null을 체크하는 법

다양한 List의 Null 체크 방법들이 있는데, 어떤 방법이 효과적일지에 대해 정리해보도록 하자. TL;DRList의 Null Check를 할 때는Spring에서 Apach Commons 라이브러리의 CollectionUtils.isEmpty()를 사용하자. Null

JihyeHwang09.github.io

 

728x90

파일첨부를 구현하는 중 사용자가 첨부하는 파일명의 중복을 없애기 위해 탐색 중 알게된 개념을 정리한다.

UUID(Universally Unique Identifier)란?

  • 위키백과 : 범용 고유 식별자(汎用固有識別子, 영어: universally unique identifier, UUID)는 소프트웨어 구축에 쓰이는 식별자 표준
  • 고유 식별자로, 주로 중복을 제거하기 위해 사용한다.
  • 만약 서로 다른 사용자가 다른 사진을 첨부했는데 서버에 저장되는 파일명이 같다면? 이런 상황을 피하기 위해 UUID를 사용할 수 있다.

 

구현 방법

String id = UUID.randomUUID().toString();
  • 생성 시 UUID 형태이므로 toString을 사용해준다.
728x90

자료형을 String으로 변환하는 방법을 기록한다.

1. 사용법

  • String.valueOf( )
Long a = 3l;
String s = String.valueOf(a);
System.out.println(s);

2. 변환 가능한 자료형

변환 가능한 자료형

 

 + 추가

  • 비슷한 사례로 int를 Long으로 변환하려 하면 Long.valueOf( int i ) 를 사용하면 된다.
  • [바뀌고자하는 자료형].valueOf( ) 로 기억하자 

'Language > Java' 카테고리의 다른 글

[Java] List Null 체크  (0) 2022.01.11
[Java] UUID란? (중복제거)  (0) 2022.01.09
[Java] String contains() 문자열 포함 여부  (0) 2022.01.03
[Java] Long, int <-> String 타입 변경  (0) 2022.01.03
[Java] String null, isEmpty() 확인  (0) 2022.01.03
728x90

문자열 포함 여부 확인 방법을 기록한다.

1. 코드

String s = "hello World";

System.out.println("e : " + s.contains("e"));
System.out.println("hello : " + s.contains("hello"));
System.out.println("oW : " + s.contains("oW"));
System.out.println("world : " + s.contains("world"));
System.out.println("World : " + s.contains("World"));
System.out.println("ld : " + s.contains("ld"));

2. 결과

결과

3. contains 결론

  • 붙어있는 문자열 사이 한 글자도 true반환
  • 공백을 구분한다.
  • 대/소문자를 구분한다.
728x90

1. Long에서 String으로 타입 변경과 String에서 Long으로 타입 변경을 기록한다.

  • Long to String
  • Long id = 3l; String s = Long.toString(id);
  • String to Long
  • String s = "3"; Long i = Long.parseLong(s);

2. int에서 String으로 타입 변경과 String에서 Long으로 타입 변경을 기록한다.

  • String to int
  • int to = Integer.parseInt(String s);
  • int to String
  • String s = Integer.toString(int i);
728x90

빈값확인 및 null확인

 

String a = "";
String b = null;

1. a의 경우

System.out.println(a.isEmpty());
  • 결과값 : true

2. b의 경우

System.out.println(b == null);
  • 결과값 : true

+ Recent posts