簡體   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