Vicky Vicky - 1 year ago 98
Java Question

Removing trailing spaces in all lines from a input file to spring batch program

I have a spring batch program which reads from a file and writes to a database.

Data is delimited by || (double pipes) and there is no delimiter at end of line.


Issue is the end of each line is having hundreds of trailing spaces before end of line character.

Using ctrl + Q, I in text pad shows:


This is throwing parsing error at each line in ItemReader (org.springframework.batch.item.file.FlatFileItemReader)

I can not remove the spaces manually and need some automation so that before the file is read by spring batch program, the spaces are gone.

How to do this ?

Note: The error occurs only when I run the program over unix server. It does not happen on windows.

<beans:bean id="myBufferedReaderFactory" class="com.mypackage.MyBufferedReaderFactory"/>

<beans:bean id="FileToDBItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<beans:property name="BufferedReaderFactory" ref="myBufferedReaderFactory"/>
<beans:property name="resource" ref="MyInputFileResource" />
<beans:property name="lineMapper">
<beans:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<beans:property name="lineTokenizer">
<beans:bean class="com.mypackage.MyCustomLineTokenizer">
<beans:property name="delimiter" value="||"/>
<beans:property name="names" value="one,two,three,four" />
<beans:property name="fieldSetMapper">
<beans:bean class="com.mypackage.MyFieldSetMapper" />

Progressed a bit. But still not hitting the answer.


import org.springframework.batch.item.file.BufferedReaderFactory;

public class MyBufferedReaderFactory implements BufferedReaderFactory {

public BufferedReader create(final Resource resource, final String encoding) throws UnsupportedEncodingException, IOException {

InputStream sourceStream = null;
sourceStream = resource.getInputStream();

//code to remove trailing spaces end

//code to remove trailing spaces start

return new BufferedReader(new InputStreamReader(sourceStream, encoding));



How can I remove the trailing spaces using InputStream object in above class ??
Thanks for reading!!

Answer Source

Extend BufferedReader by implementing BufferedReaderFactory and add line-trimming functionality to the reader. See FlatFileItemReader.setBufferedReaderFactory().

public class LineTrimmingBufferedReader extends BufferedReader
    public LineTrimmingBufferedReader(Reader in)

    public String readLine() throws IOException
        String result = super.readLine();
        return result != null ? result.trim() : null;

Implement proper trailing space trimming of the line if String.trim() isn't acceptable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download