Spring FlatFileItemWriter package does not write data to file

I am new to Spring batch application. I am trying to use FlatFileItemWriterto write data to a file. The task - this application creates a file at a given path, but now writes the actual content to it.

The following are details related to the code:

List<String> dataFileList : This list contains the data that I want to write to the file.

FlatFileItemWriter<String> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("C:\\Desktop\\test"));
writer.open(new ExecutionContext());
writer.setLineAggregator(new PassThroughLineAggregator<>());
writer.setAppendAllowed(true);
writer.write(dataFileList);
writer.close();

It just generates the file in the right place, but the content is not written to the file.

Am I missing something? Help is much appreciated.

Thank!

+4
source share
2 answers

This is not the right way to use Spring Batch Writer and write data. First you need to declare a bean from Writer.

  • Define Bean Mission
  • Determine Bean Step
  • Writer bean

:

https://github.com/pkainulainen/spring-batch-examples/blob/master/spring-boot/src/main/java/net/petrikainulainen/springbatch/csv/in/CsvFileToDatabaseJobConfig.java

https://spring.io/guides/gs/batch-processing/

+2

, , . https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/FlatFileItemWriter.html,

setForceSync

public void setForceSync(boolean forceSync)

Flag to indicate that changes should be force-synced to disk on flush. Defaults to false, which means that even with a local disk changes could be lost if the OS crashes in between a write and a cache flush. Setting to true may result in slower performance for usage patterns involving many frequent writes.

Parameters:
    forceSync - the flag value to set
+1

Source: https://habr.com/ru/post/1691785/


All Articles