지식정보
[JAVA] 파일확장자 제거, 가져오기 (with Commons IO)
onggun
2021. 2. 5. 15:26
파일명에서 확장자를 가져오거나 제거할 때 유용한 코드를 기록으로 남긴다.
/* 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