spring-batch - Spring批处理绝对路径
问题描述
我有一个 Spring Batch 进程,我想使用相对路径来使用 csv 文件编写 Spring Batch 异常,但它拒绝,它缺少绝对路径,为什么?
文件是:fo
我的代码:
public class MyJob {
File fo=new File("C:\\Users\\m.youneb\\Documents\\icdc\\cecWorkplace\\saveLines\\src\\main\\resources\\csv\\skip.csv");
@Bean
public Step step() throws IOException {
return steps.get("step")
.<Person, Person>chunk(5)
.reader(itemReader())
.processor(itemProcessor())
.writer(itemWriter())
.faultTolerant()
.skip(IllegalArgumentException.class)
.skip(FlatFileParseException.class)
.skipLimit(100)
.listener(new MySkipListener(fo))
.skip(Exception.class)
.build();
}
public static class MySkipListener implements SkipListener<Person, Person> {
//private FileWriter fileWriter;
private BufferedWriter bw = null;
public MySkipListener(File file) throws IOException {
//this.fileWriter = new FileWriter(file);
bw= new BufferedWriter(new FileWriter(file, true));
System.out.println("MySkipListener =========> :"+file);
}
@Override
public void onSkipInRead(Throwable throwable) {
if (throwable instanceof FlatFileParseException) {
FlatFileParseException flatFileParseException = (FlatFileParseException) throwable;
System.out.println("onSkipInRead =========> :");
try {
bw.write(flatFileParseException.getInput()+"Vérifiez les colonnes!!");
bw.newLine();
bw.flush();
// fileWriter.close();
} catch (IOException e) {
System.err.println("Unable to write skipped line to error file");
}
}
}
}
我需要使用相对路径,谢谢。
解决方案
要使用文件的相对路径,您可以使用:
File fo = new File("src/main/resources/csv/skip.csv"); // should work on windows
这假设您在包含 src/main/resources
的目录中运行 JVM,该目录通常是典型 maven 项目的根目录。
希望这可以帮助。
推荐阅读
- html - 在引导程序和 HTML 中调整大小时停止图像碰撞
- python - BeautifulSoup - 在特定模式中拆分抓取的字符串并保存到数据框
- java - 如何为这两个表编写查询
- reactjs - React.useState 如何再次触发包含函数?
- javascript - 为什么从 python 执行时无法识别 CSS 和 Javascript?
- linux - Linux内核:错误'函数'getuid'的隐式声明
- ruby-on-rails - 将 Logger 输出复制到 production.log 和控制台以执行 Rake 任务
- twitter-bootstrap - 使用 bootstrap3-dialog 包通过使用模板字符串在 Bootstrap 对话框中显示消息
- android - Android 布局 - NestedScrollView 与 AppBarLayout 重叠
- php - HERE REST API OAuth 2.0 令牌请求(带有 1.0 标头)错误