파일에서 데이터를로드하려고하는데 두 파일의 IP 주소와 포트가 유지됩니다.Java : .yaml 파일 구문 분석 [가능한 경우 snake.yaml 사용]
config.yaml
ip_addresses :
firstMachineAddress : '162.242.195.82'
secondMachineAddress : '50.31.209.229'
ports :
firstTargetPort : '4041'
secondTargetPort : '5051'
그리고 내가에서로드 할 (하지 클래스와 같은 패키지에 )를 프로젝트 폴더 아래에이 장소 다음과 같이 내 .yaml
파일입니다 . 그러나, 내가 지금까지 시도한 것은 무엇이든 작동하지 않았다.
마지막으로, 나는 src/main/resources
아래의 설정 파일을 넣고이 시도 :
private void loadFromFile() throws FileNotFoundException {
Yaml yaml = new Yaml();
ArrayList<String> key = new ArrayList<String>();
ArrayList<String> value = new ArrayList<String>();
try {
InputStream ios = getClass().getResourceAsStream("/config.yaml");
// Parse the YAML file and return the output as a series of Maps and Lists
Map<String, Object> result = (Map<String, Object>) yaml.load(ios);
for (Object name : result.keySet()) {
key.add(name.toString());
value.add(result.get(name).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(key + " : " + value);
}
을하지만,이 또한 작동하지 않았다. 나는 "Stream Closed"오류가 발생하고 아무것도로드하지 않습니다.
누구든지이 문제를 해결할 사람을 알고 있습니까?
추신 : 나는 snake-yaml 라이브러리를 사용하라고 말했고, 중요한 경우를 대비해 다른 파일을 다운로드 할 수 없습니다.
Java에서 프로젝트 폴더 아래에있는 문서에 도달하는 방법이 있습니까? –
런타임시 Java에는 프로젝트 폴더 개념이 없습니다. .getResource() 메소드는 ClassLoader와 클래스 패스를 기반으로하는 함수 다. Class.getResource()를 호출 클래스에 대한 상대 경로와 함께 사용하거나 ClassLoader.getResource()에 classpath와 관련된 경로를 사용할 수 있습니다. –