繁体   English   中英

使用Spring Batch编写多个文件

[英]Writing multiple files with Spring Batch

我是Spring Batch的新手,我很感激一些帮助来解决这种情况:我用MultiResourceItemReader读取一些文件,进行一些编组工作,在ItemProcessor中我收到一个String并返回一个Map<String, List<String>> ,所以我的问题是在ItemWriter中我应该迭代Map的键,并且为每个键生成一个包含与该键相关的值的新文件,有人可以指出我正确的方向以便创建文件?
我也在使用MultiResourceItemWriter,因为我需要生成最多行的文件。
提前致谢

好吧,最终得到了一个解决方案,我对此并不感到兴奋,但它有效,我没有太多时间,所以我扩展了MultiResourceItemWriter并重新定义了“write”方法,处理了map的元素并编写了我自己的文件。 如果有人在那里需要它,这里是。

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

就是这样,我想相信有一个更好的选择,我不知道,但目前这是我的解决方案。 如果有人知道如何增强我的实现,我想知道它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM