Properties
클래스를 사용하여 HashMap
을 채우려고합니다. .propeties
파일의 항목을로드 한 다음 HashMap
에 복사하고 싶습니다.속성 파일의 항목으로 HashMap 채우기
이전에 속성 파일로 HashMap
을 초기화했지만 지금은 HashMap
을 이미 정의했으며 생성자에서만 초기화하려고합니다.
이전 방법 :
Properties properties = new Properties();
try {
properties.load(ClassName.class.getResourceAsStream("resume.properties"));
}
catch (Exception e) {
}
HashMap<String, String> mymap= new HashMap<String, String>((Map) properties);
하지만 지금은 내가
public class ClassName {
HashMap<String,Integer> mymap = new HashMap<String, Integer>();
public ClassName(){
Properties properties = new Properties();
try {
properties.load(ClassName.class.getResourceAsStream("resume.properties"));
}
catch (Exception e) {
}
mymap = properties;
//The above line gives error
}
}
가 어떻게이 속성을 할당 할이 여기에 HashMap
에 반대 있나요?
같이 getResourceAsStream를 사용할 때 ** 널 (null)을 받고있어 * *, 아마도 클래스 패스 밖에서 파일을 찾고 있기 때문일 것이다. 따라서 절대 파일 경로를 읽으려면 다음을 할 수 있습니다 : \t \t'File file = new File ("absolute/file/path/resume.properties"); \t \t \t FileInputStream fileInputStream = 새 FileInputStream (파일); \t \t \t properties.load (fileInputStream); \t \t \t fileInputStream.close(); ' –