2012-06-26 3 views
4

저는 스프링 배치의 초보자입니다.이 상황을 해결하는 데 도움이되어 주시면 감사하겠습니다 : MultiResourceItemReader로 파일을 읽고, 마샬링 작업을하고, ItemProcessor에서 String과 Map<String, List<String>> 반환, 내 문제는 해당 ItemWriter Map의 키를 반복해야하며 각각의 해당 키와 관련된 값을 포함하는 새 파일을 생성 할 수, 누군가가 올바른 방향으로 나를 가리킨 수 있습니다. 파일을 만드시겠습니까?
최대 줄 수의 파일을 생성해야하기 때문에 MultiResourceItemWriter도 사용하고 있습니다. 사전에
덕분에Spring Batch로 여러 파일 작성하기

답변

1

음, 마침내 나는 그것에 대해 정말 흥분하지 않다 그러나 그것은 작동하고 내가 훨씬 더 많은 시간을 가지고 있지 않기 때문에 나는 MultiResourceItemWriter을 확장하고 "쓰기"재정의 한, 솔루션을 가지고 메서드를 사용하여지도 요소를 처리하고 파일을 직접 작성합니다. 거기 밖으로 누군가가 그것을 필요로하는 경우에, 여기 있습니다.

@Override 
    public void write(List items) throws Exception { 

     for (Object o : items) { 
       //do some processing here 

       writeFile(anotherObject); 
     } 

    private void writeFile (AnotherObject anotherObject) throws IOException { 
     File file = new File("name.xml"); 
     boolean restarted = file.exists(); 
     FileUtils.setUpOutputFile(file, restarted, true, true); 

     StringBuffer sb = new StringBuffer(); 
     sb.append(xStream.toXML(anotherObject)); 

     FileOutputStream os = new FileOutputStream(file, true); 

     BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, Charset.forName("UTF-8"))); 
     bufferedWriter.write(sb.toString()); 
     bufferedWriter.close(); 
    } 

그리고 그게 더 좋은 옵션이 있다는 것을 알고 싶지만, 지금은 내 해결책입니다. 구현을 향상시킬 수있는 방법을 알고 있다면 누구나 알고 싶습니다.

관련 문제