首页 > 解决方案 > 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");
                }
            }
        }
 }

我需要使用相对路径,谢谢。

标签: spring-batch

解决方案


要使用文件的相对路径,您可以使用:

File fo = new File("src/main/resources/csv/skip.csv"); // should work on windows

这假设您在包含 src/main/resources的目录中运行 JVM,该目录通常是典型 maven 项目的根目录。

希望这可以帮助。


推荐阅读