Mastering Spring Batch: Read & Write Flat Files (XML, CSV, TXT)

This tutorial walks through using Spring Batch to read and write flat files—including CSV, TXT, and XML—by configuring Maven dependencies, setting up a Spring Boot project structure, and implementing FlatFileItemReader, FlatFileItemWriter, and custom ItemProcessor components with concrete code examples.

Programmer DD
Programmer DD
Programmer DD
Mastering Spring Batch: Read & Write Flat Files (XML, CSV, TXT)

Prerequisites and Dependencies

The example uses Spring Batch 3.0.7 (with some 4.0.x features) on Spring Boot. Required Maven dependencies are:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-oxm</artifactId>
  <version>4.3.8.RELEASE</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>

Project Structure

The code is organized by responsibility:

launcher – launches and invokes the job.

processor – contains data transformation logic.

FlatFileItemReader

FlatFileItemReader reads text‑based files (CSV, TXT) and maps each line to a domain object. The essential properties are: setResource – location of the input file (e.g., ClassPathResource("data/sample-data.csv")). setLineMapper – defines how a line is tokenized and mapped; the example uses DefaultLineMapper with a DelimitedLineTokenizer and a BeanWrapperFieldSetMapper. setEncoding – defaults to iso-8859-1. setStrict – true by default; throws an exception if the file is missing.

Configuration example:

@Bean
public FlatFileItemReader<Person> csvItemReader() {
    FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("data/sample-data.csv"));
    reader.setLineMapper(new DefaultLineMapper<Person>() {{
        setLineTokenizer(new DelimitedLineTokenizer() {{
            setNames(new String[]{"name", "age"});
        }});
        setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
            setTargetType(Person.class);
        }});
    }});
    return reader;
}

FlatFileItemWriter

FlatFileItemWriter writes a stream of objects to a text file. Important methods include: setLineAggregator – converts an object to a delimited string; common implementations are DelimitedLineAggregator (comma‑separated) and PassThroughLineAggregator. setResource – output file location (e.g., ClassPathResource("/data/sample-data.txt") or FileSystemResource). setEncoding – defaults to iso-8859-1, but can be set to UTF-8.

Configuration example:

@Bean
public FlatFileItemWriter<Person> txtItemWriter() {
    FlatFileItemWriter<Person> writer = new FlatFileItemWriter<>();
    writer.setAppendAllowed(true);
    writer.setEncoding("UTF-8");
    writer.setResource(new ClassPathResource("/data/sample-data.txt"));
    writer.setLineAggregator(new DelimitedLineAggregator<Person>() {{
        setDelimiter(",");
        setFieldExtractor(new BeanWrapperFieldExtractor<Person>() {{
            setNames(new String[]{"name", "age"});
        }});
    }});
    return writer;
}

XML File Handling

Processing XML files requires the spring-oxm dependency. Writing XML uses StaxEventItemWriter, which mirrors FlatFileItemWriter with methods such as setRootTagName, setMarshaller, and setResource. The default encoding is UTF‑8.

Custom ItemProcessor

The ItemProcessor transforms input objects before they are written. The example increments the age field and appends a suffix to the name:

public class PersonItemProcessor implements ItemProcessor<Person, Person> {
    @Override
    public Person process(Person person) throws Exception {
        person.setAge(person.getAge() + 1);
        person.setName(person.getName() + "-_-" );
        return person;
    }
}

Job Flow Overview

The complete job processes data in three stages:

Read a CSV file.

Transform the data with ItemProcessor.

Write the result to a TXT file.

Read the generated TXT file.

Transform again if needed.

Write the final output as an XML file.

This sequence demonstrates how Spring Batch can move data between different flat‑file formats and XML while handling encoding, strict file existence checks, and transactional guarantees.

Job flow diagram
Job flow diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

XMLCSVflat fileSpring Batch
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.