프로그램/JAVA

[JAVA] 파일 이름, 경로, 확장자명 나누기

프뚜 2022. 10. 3. 16:58
728x90
반응형
SMALL

안녕하세요!

프뚜(프로그래머 뚜)입니다!

 

[개발 환경]

 - OS: windows 10 64bit

 - JAVA: v1.8

 - SpringBoot: v2.7.3

 

파일을 업로드 또는 다운로드 할 때 파일의 이름, 경로, 확장자 등을 나눠서 사용할 때가 많습니다. 예전엔 특수 기호 등으로 나눠서 체크했지만 FilenameUtils를 통해 간편하게 나눌 수 있어서 포스팅하게 되었습니다.

 

 

1. build.gradle > common-io implementation 하기
# 2022.10.03[프뚜]: FilenameUtils import
implementation 'commons-io:commons-io:2.11.0'

 

 

2. AbsolutePath 나누기
String absolutePath = "D:\\ssjeong\\프뚜.png";
String name = FilenameUtils.getName(absolutePath);
String fullPath = FilenameUtils.getFullPath(absolutePath);
String baseName = FilenameUtils.getBaseName(absolutePath);
String extension = FilenameUtils.getExtension(absolutePath);

# 2022.10.03[프뚜]: 확장자를 제거한 순수 파일명
System.out.println("[프뚜] > 파일명: " + baseName);

# 2022.10.03[프뚜]: 파일명을 제거한 순수 확장자
System.out.println("[프뚜] > 확장자: " + extension);

# 2022.10.03[프뚜]: 파일이 있는 경로
System.out.println("[프뚜] > 파일경로: " + fullPath);

# 2022.10.03[프뚜]: 파일의 이름과 확장자
System.out.println("[프뚜] > 파일명 + 확장자: " + name);

commons-io를 사용하면 보다 편리하게 파일 경로, 파일명, 확장자 등을 나눌 수 있습니다.

 

해당 프로젝트는 gitlab에 제공되고 있습니다. (포스팅 제목과 Git History는 1:1 매칭입니다.)
https://github.com/JeongSeongSoo/spring-tistory.git

 

GitHub - JeongSeongSoo/spring-tistory

Contribute to JeongSeongSoo/spring-tistory development by creating an account on GitHub.

github.com


궁금하신 부분 또는 문제가 생긴 부분에 대해서 댓글 남겨주시면 빠르고 정확한 답변드리겠습니다.

728x90
반응형
LIST