2014-04-25 2 views
0

나는 snakeyaml을 사용하여 yaml 파일을 읽는 간단한 자바 프로그램을 가지고 있으며 yaml 객체의 일부 값을 편집했다. 그러나 개체를 다시 yaml 파일로 덤프 할 수 없습니다. yaml 객체를 파일에 덤핑하는 올바른 방법이 무엇인지 모릅니다. Heres는 코드파일에 Yaml 객체를 덤프하는 방법은 무엇입니까?

 InputStream input = new FileInputStream(new File("/etc/cassandra/cassandra.yaml")); 
     Writer output = null; 
     try { 
      output = new FileWriter("/etc/cassandra/cassandra.yaml"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Yaml yaml = new Yaml(); 
     Map<String, Object> data = (Map<String, Object>) yaml.load(input); 
     System.out.println("yaml data is " +data.get("saved_caches_directory")); 
     data.put("saved_caches_directory", "/var/lib/cassandra/saved_new_caches"); 
     yaml.dump(data, output); 
     System.out.println(output.toString()); 
     output.write(data); // Error in this line. 

    } 

나는 다시 파일에 수정 된 YAML 개체를 덤프 yaml.dump을()가 필요합니다. 나는 그곳에서 어떻게 글쓰기가 일어나는 지 모르겠습니다. eclipse yaml.dump (data, output)에 따르면; 메서드는 출력이 writer 객체 인 경우 사용할 수 있습니다. 쓰기 객체를 파일로 다시 보내도록 인스턴스화하는 방법을 잘 모르겠습니다. 당신이

writer.close(); 

을 완료하면

답변

1

이 작가를 닫으려면 작가

yaml.dump(data, writer); 

에 데이터를 덤프하려면 작가

Writer writer = new FileWriter("/etc/cassandra/cassandra.yaml"); 

를 만들려면

+1

나는 같은 이전 시도했다 않았다 . 내 문제를 찾은 것 같아. 필자의 입력 스트림과 출력 작성자는 하나씩 초기화되었습니다. 어떤 이유로 내 파일이 잘려서 읽을 내용이 없었습니다. .. – Rahul

+0

@Rahul 동의하지 않으면, 당신이 문서를 작성하고 닫을 때까지 읽기 용 파일을 열지 마십시오. –

+0

나는 그 역도 마찬가지라고 생각합니다. 수업은 배웠다! – Rahul

관련 문제