Record multiple files with Spring Batch

I am new to Spring Batch, and I would be grateful for help in resolving this situation: I read several files using MultiResourceItemReader, do some sorting work, in ItemProcessor I get a string and return Map<String, List<String>> , so mine the problem is that in ItemWriter I have to iterate over the keys of the Map and for each of them it generates a new file containing the value associated with this key, can someone point me in the right direction to create the files?
I also use MultiResourceItemWriter because I need to generate files with the maximum number of lines.
Thanks in advance

+4
source share
1 answer

Well, finally, the solution came out, I’m not very excited, but it works, and I don’t have much time, so I expanded MultiResourceItemWriter and redefined the β€œwrite” method, processing the map elements and writing the files myself. In case someone needs it, here it is.

  @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(); } 

And what is it, I want to believe that there is a better option that I do not know, but at the moment this is my decision. If anyone knows how I can improve my implementation, I would like to know.

+1
source

All Articles