ClassLoader를 이용하면 상대 경로를 이용할 수 있어서 이것저것 편한 점이 많다.
실제로 아래 sample.properties는 class path 내에 common 패키지 아래에 위치해 있다.
URL url1 = ClassLoader.getSystemResource("common/sample.properties");Properties prop1 = new Properties();URL url2 = this.getClass().getResource("/common/sample.properties");Properties prop2 = new Properties();try {prop1.load(url1.openStream());System.out.println(prop1);prop2.load(url2.openStream());System.out.println(prop2);} catch (IOException e) {e.printStackTrace();}
상대 경로를 사용하지 않는다면 "C:\Files...\sample.properties"와 같이 사용해야 하므로 Resource를 가져올 때는 반드시 ClassLoader를 이용하는 것이 현명하다.
사진, 오디오 등 어떤 종류의 Resource도 읽어 올 수 있으니 참고 하자!
공유하기 버튼
|
|




덧글