Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring批处理写入多个文件_Java_Spring_Spring Batch - Fatal编程技术网

Java 使用Spring批处理写入多个文件

Java 使用Spring批处理写入多个文件,java,spring,spring-batch,Java,Spring,Spring Batch,我是Spring Batch的新手,我希望能得到一些帮助来解决这种情况:我使用MultiResourceItemReader读取一些文件,进行一些编组工作,在ItemProcessor中,我收到一个字符串并返回一个映射,因此,我的问题是,在ItemWriter中,我应该迭代映射的键,并为每个键生成一个新文件,其中包含与该键相关的值,是否有人可以为我指出正确的方向,以便创建这些文件? 我还使用MultiResourceItemWriter,因为我需要生成最多行的文件。 提前感谢好吧,终于找到了一个

我是Spring Batch的新手,我希望能得到一些帮助来解决这种情况:我使用MultiResourceItemReader读取一些文件,进行一些编组工作,在ItemProcessor中,我收到一个字符串并返回一个
映射
,因此,我的问题是,在ItemWriter中,我应该迭代映射的键,并为每个键生成一个新文件,其中包含与该键相关的值,是否有人可以为我指出正确的方向,以便创建这些文件?
我还使用MultiResourceItemWriter,因为我需要生成最多行的文件。

提前感谢

好吧,终于找到了一个解决方案,我对它不是很感兴趣,但它正在工作,我没有太多时间,所以我扩展了MultiResourceItemWriter并重新定义了“write”方法,自己处理地图元素并编写文件。 如果外面有人需要它,就在这里

    @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();
    }
就这样,我想相信有一个更好的选择,我不知道,但目前这是我的解决方案。如果有人知道如何改进我的实现,我很想知道