Java Reference
In-Depth Information
State
2
The two letter state abbreviation.
Zip Code
5
The customer's postal code.
Defining the format for a fixed with file is important. A delimited file describes its fields with its
delimiters. XML or other structured files are self-describing given the metadata the tags provide.
Database data has the metadata from the database describing it. However, fixed-width files are different.
They provide zero metadata to describe their format. If you look at Listing 7-1, you can see an example of
what the previous description looks like as your input file.
Listing 7-1. customer.txt, the Fixed-Width File
Michael TMinella 123 4th Street Chicago IL60606
Warren QGates 11 Wall Street New York NY10005
Ann BDarrow 350 Fifth Avenue New York NY10118
Terrence HDonnelly 4059 Mt. Lee Drive HollywoodCA90068
To demonstrate how each of these readers work, you will create a single-step job that reads in a file
and writes it right back out. For this job, copyJob, you will create a copyJob.xml file with the following
beans:
customerFile : The input file.
outputFile : The file you will copy the input file to.
customerReader : The FlatFileItemReader.
outputWriter : The FlatFileItemWriter.
copyStep : The step definition for your job.
copyJob : The job definition.
Your customerFile and outputFile beans will be nothing more than Spring's
org.springframework.core.io.FileSystemResource beans. Each of the file-related beans has the scope
step because they can't be bound until the step begins (unlike normal Spring beans, which are
instantiated and wired upon the application's startup) 1 . The customerReader is an instance of the
FlatFileItemReader. As covered previously, the FlatFileItemReader consists of two pieces, a resource to
read in (in this case, the customerFile) and a way to map each line of the file (a LineMapper
implementation).
Note The “step” scope is a new bean scope provided by Spring Batch. This scope allows bean properties to be
set when the step is excuted instead of on application startup (as is the default in Spring).
1 Late binding of JobParameters was discussed in Chapter 4.
 
Search WWH ::




Custom Search