Eclipse에서 동적 웹 프로젝트를 만들고 속성 파일에서 읽을 수있는 간단한 코드를 작성했습니다. 나는 예외를 파일을 찾을 수 없습니다있어이 작업을 실행하면
여기에 내 코드속성 파일에서 값 읽기
public class AutocompleteService {
public static void main (String args[])
{
Properties properties = new Properties();
properties.load(new FileInputStream("autocomplete.properties"));
System.out.println("Test : " + properties.getProperty("test"));
}
입니다.
java.io.FileNotFoundException: autocomplete.properties (The system cannot find the file specified)
내 패키지 구조는
-src
- com.serive (package)
- AutocompleteService.java
- autocomplete.properties
두 AutocompleteService.java
다음과 같이하고 autocomplete.properties
우리가 파일 속성에서 읽기 위해 아무것도 할 필요가 즉 com.service
수도 있었죠 동일한 패키지에?
참조 : http://www.exampledepot.com/egs/java.util/Props.html
~ Ajinkya. 당신이 스트림이있을 때
ClassLoader loader = Thread.currentThread().getContextClassLoader();
stream = loader.getResourceAsStream(fileName);
당신이) (귀하의 코드는 "현재 디렉토리"에 찾고
고마워요 :) 그것은 효과적이고 다른 사람들보다 덜 복잡합니다.:) – xyz
은'getResourceAsStream'이어야합니다. – Thilo
@Thilo : 그래야합니다 .' properties.load (AutocompleteService.class.getResourceAsStream ("autocomplete.properties")); ' – xyz