Java 파일명, 경로 추출 문제

박현우·2024년 1월 5일

업로드중..

D:/photo/2023/travel/food.jpg라는
파일이 있다. 이 파일의 경로를 "data"라는 객체로
생성한 뒤에, 다음의 형태로 출력하시오
(split() 메서드 사용 금지)
--------------------------------
[출력결과]
파일이름 : food
확장자 : jpg
폴더명 : D:/photo/2023/travel
*/

package string;

public class Main05 {

public static void main(String[] args) {
	
	String data = "D:/photo/2023/travel/food.jpg";
	
	// 파일 이름 추출
	String name = 
			data.substring(data.lastIndexOf("/")+1, data.lastIndexOf("."));
	System.out.println("파일이름 : " + name);
	// 확장자 추출
	String ext = data.substring(data.indexOf(".")+1);
	System.out.println("확장자 : " + ext);
	
	// 소스 파일의 폴더 이름 추출
	System.out.println("폴더명: " + data.substring(0, data.lastIndexOf("/")));
	
	
}

}

0개의 댓글