简体   繁体   English

为什么在春季批处理中不使用FlatFileItemWriter创建文件?

[英]Why file not creating using FlatFileItemWriter in spring batch?

I am using spring batch to write a bean into csv file. 我正在使用spring batch将bean写入csv文件。 But file is not getting created and I am not getting errors either. 但是文件没有被创建,我也没有得到错误。 I tried using close() for fileWriter also. 我也尝试使用close()作为fileWriter Following is my writer part of code. 以下是我的作者代码的一部分。

public void writeToCSVFile(User user) throws IOException{
    Resource src = new FileSystemResource("output.csv");
    try{
        fileWriter.setResource(src);

        fileWriter.setAppendAllowed(true);
        fileWriter.setForceSync(true);
        fileWriter.setLineAggregator(new DelimitedLineAggregator<User>() {
            {
                setDelimiter(",");
                setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
                    {
                        setNames(new String[] { "employeeId", "userName", "fullName"});
                    }
                });
            }
        });
    } catch(Exception e){
        System.out.println("Error in logging file" + e.getMessage());
    }
}

You have set the AppendAllowed flag to true, so probably the file already exists and hence it is not created. 您已将AppendAllowed标志设置为true,因此该文件可能已经存在,因此无法创建。

Apart from that, I'm not sure your configuration is correct. 除此之外,我不确定您的配置是否正确。 You need to define your item writer as a bean and register it within a chunk-oriented step in a job. 您需要将项目编写器定义为Bean,并在作业中面向块的步骤中注册它。 The step will open the writer, write some data, and close it when appropriate. 该步骤将打开编写器,写入一些数据,并在适当时关闭它。 You can find many samples in the spring-batch-samples module . 您可以在spring-batch-samples模块中找到许多示例。

Otherwise, you need to open the writer, call the write method to write some data, and then close the writer yourself. 否则,您需要打开write ,调用write方法写入一些数据,然后自己关闭编写器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FlatFileItemWriter在Spring批处理中在异常时生成空白文件 - FlatFileItemWriter generating blank file on exception in Spring batch Spring Batch-FlatFileItemWriter写入文件,关闭该文件,写入新文件 - Spring Batch - FlatFileItemWriter write to a file, close it, write to new file Spring Batch FlatFileItemWriter - 输出自定义 - Spring Batch FlatFileItemWriter - output customization 如何在Spring批处理中使用FlatFileItemWriter写入stdout? - How can I write to stdout using FlatFileItemWriter in spring batch? 使用批处理作业的FlatFileItemReader和FlatFileItemWriter - FlatFileItemReader and FlatFileItemWriter Using Batch Job Spring 批量 FlatFileItemWriter 从 Object 写入 csv - Spring batch FlatFileItemWriter write as csv from Object Spring Batch:无法访问flatfileitemwriter中的jobexecutionConext - Spring Batch: not able to access jobexecutionConext in flatfileitemwriter 如何在使用 Spring Batch FlatFileItemWriter 写入 csv 文件时跳过空白行 - how to skip blank lines while writing to csv file with Spring Batch FlatFileItemWriter Spring 批处理 FlatFileItemWriter - 写入后在 csv 中显示指数值 - Spring batch FlatFileItemWriter - Showing exponential value in csv after writing it Spring Batch FlatFileItemWriter仅在第二次运行后才处理数据 - Spring Batch FlatFileItemWriter does only process data after second run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM