首页 > 解决方案 > How to read text files(.txt) from file system using reader and pass each file's text to processor spring batch?

问题描述

I wanted to create spring batch project that which can read files(.txt only) from a file system and pass the text of each file to the processor. Suppose i will use that text to covert uppercase in processor and wanted to save in single using custom writer.

标签: javaspringspring-batch

解决方案


要从文件系统读取文件,您可以尝试以下方法并可以根据您的实际要求进行修改,在内部它都是相同的,只是您想要采用哪种方式,这种方法是 JAVA 方式,不针对任何框架.

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileExample1 {

    public static void main(String[] args) {

       StringBuilder sb = new StringBuilder();
       String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt");              
        try (BufferedReader br = Files.newBufferedReader(Paths.get("filename.txt"))) {

            // read line by line
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }

        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }

        System.out.println(sb);

    }

}

推荐阅读