java - 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.
解决方案
要从文件系统读取文件,您可以尝试以下方法并可以根据您的实际要求进行修改,在内部它都是相同的,只是您想要采用哪种方式,这种方法是 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);
}
}
推荐阅读
- c++ - C ++中的正则表达式unicode字符
- c# - Windows 窗体 C# 中的控制台
- r - IF(AND 公式使用 R
- python - 集合如何在python内部避免重复?
- flutter - 覆盖自定义颤振小部件
- html - CSS Safari 14 出现问题,当包含在弹性框内的网格中时,子元素会爆炸
- javascript - 删除深层嵌套一维数组中的所有嵌套子节点
- reactjs - 如何检查点击事件是否发生在特定元素内部或外部
- asp.net-core - MVC Net Core 3.1:创建和下载 PDF 文件的工具
- node.js - 如何在 nodejs 14.15 中运行 WebAssembly