问题
I am using spring batch for my application. Basically, all I have to do is read all XML files from the input directory. When I get all the XML files, each file needs to be converted to root Object. That object I have to split into two objects and print them into CSV file. So, if there are 10 XML files in the input folder, I need to generate CSV with 20 lines. If there are 150, CSV will have 300 lines, etc. I meant to use FileReadingMessageSource. It has a queue. But the problem with this is the return type of that queue. It returns File, and what I need is the List of queues. Can I somehow get the entire list of XML files from a directory in my reader?
回答1:
IMO you don't need to use the FileReadingMessageSource (and introduce Spring Integration) in your basic use case.
You can create a reader that returns a File, a processor that maps the File to your root Object (and split in two objects) and finally a FlatFileItemWriter to generate the CSV output.
EDIT: add reader example:
@Bean
public ItemReader<File> itemReader() throws IOException {
List<File> files = Files.walk(Paths.get("/path/to/directory"))
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
return new IteratorItemReader<>(files);
}
来源:https://stackoverflow.com/questions/53854446/read-all-files-from-directory-spring-batch-filereadingmessagesource