[英]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.