티스토리 뷰

파일명에서 확장자를 가져오거나 제거할 때 유용한 코드를 기록으로 남긴다.

/* lastIndexOf와 substring 이요 */
File f = new File("test.txt");
int dot = f.getName().lastIndexOf('.');
if(dot > -1) {
  //확장자 
  System.out.println(f.getName().substring(dot+1);
  //확장자 제거
  System.out.println(f.getName().substring(0, dot);
}

위의 코드를 아래처럼 간단하게 처리 가능하다.

import org.apache.commons.io.FilenameUtils;

File f = new File("test.tif");
//학장자 가져오기
System.out.println( FilenameUtils.getExtension(f.getName()));
//확장자 제거
System.out.println( FilenameUtils.removeExtension(f.getName()));

Commons IO 2.6 버전에서 테스트 됨.

 

 

 

 

https://commons.apache.org/proper/commons-io/

 

Commons IO – Commons IO Overview

Commons IO Commons IO is a library of utilities to assist with developing IO functionality. There are six main areas included: io - This package defines utility classes for working with streams, readers, writers and files. comparator - This package provide

commons.apache.org

 

'지식정보' 카테고리의 다른 글

[AIX] 열려있는 포트 수 확인  (0) 2021.11.17
[Windows] 탐색기에서 현재위치로 터미널창 열기  (0) 2021.03.03
JWT (Json Web Token) 인증방식  (0) 2021.01.29
netstat 사용법  (0) 2021.01.08
Oracle FETCH  (0) 2020.05.11
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday