언어/Java

[java] properties 파일 사용해보기

antoroong 2025. 2. 8. 00:50

프로젝트 이름 : N

IDE : 이클립스

DB : mysql

SQL : ibatis

OS : centos 7

server : tomcat 6

jdk : 1.6

프레임워크 : 전자정부프레임워크 3.8

(1) 파일 업로드나 다운로드를할때 경로를 기억할 수 없어서 미리 properties라는 파일에 메모를 해두고 이후에 자바에서 그 경로를 가져오기 위해서 사용한다.

  • 이 포스팅은 PDF뷰어를 열때, 파일을 불러오기 위한 작업으로 하다가 포스팅 했습니다.

 

준비물


  •  test.properteis 파일 생성
  • test.class 파일 생성

 

1. properties파일 생성

File을 선택하여 Next 버튼

 

 

 

나는 test.properties 로 하고 Finish

 

 

저장할 경로나 구분자 같은 것을 위처럼 메모한다

 

 

2. Controller에 비즈니스 로직 설정

public String pdfViewer() {
		System.out.println("::: PDF 뷰어 Part1");
		
		
		// properties에 선언한 파일 경로 사용
		Properties properties = new Properties();
		String propFileName = "test.properties";	//가져올 properties 파일이름

		InputStream IS = getClass().getClassLoader().getResourceAsStream(propFileName);
		
		try {
			properties.load(IS);
		} catch (IOException e) {
			e.printStackTrace();
		}

		
		String gubun = properties.getProperty("file.path.A"); //test.properties < 'file.path.A' key값
		String ext = "pdf";	//파일 확장자
		
		String file_name = gubun+"."+ext;	//정의할 파일 이름
		
		String file_real_path = properties.getProperty("file.path");	// test.properties < 'file.path'  key값
		
		String file_path = file_real_path + "/" +file_name; // 파일 경로 완성
		
		System.out.println("::: gubun =>"+gubun);
		System.out.println("::: ext =>"+ext);
		System.out.println("::: file_name =>"+file_name);
		System.out.println("::: file_real_path =>"+file_real_path);
		System.out.println("::: file_path =>"+file_path);
		
		
		
		return SUCCESS;
	}

 

 

콘솔 결과 출력 완성~