Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:15:34+00:00 2026-05-27T04:15:34+00:00

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

  • 0

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.

one||two||three||four
foo||bar||foo1||bar1

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:

one||two||three||four.......................................
foo||bar||foo1||bar1...............
car||bike||tango||charlie..........................

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:bean>
                </beans:property>
                <beans:property name="fieldSetMapper">
                    <beans:bean class="com.mypackage.MyFieldSetMapper" />
                </beans:property>
            </beans:bean>
        </beans:property>
    </beans:bean>

Progressed a bit. But still not hitting the answer.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.springframework.batch.item.file.BufferedReaderFactory;
import org.springframework.core.io.Resource;


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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:15:35+00:00Added an answer on May 27, 2026 at 4:15 am

    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)
        {
            super(in);
        }
    
        @Override
        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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a spring batch program which reads from a database and writes to
I have a simple spring batch program which reads data from a INPUT file
Background I have a Spring batch program that reads a file (example file I
I have one spring batch application which can be run from command line. The
I have a spring batch application which loads multiple files into several data tables.
I wanted to run a .cmd file from Java. I have something which works
I have a program x.exe it waits for an input from user and reads
I have a java program which does individual jobs e.g. takes in a file,
I have implemeneted a method into my c# program to run a batch file,
I have a Spring Batch application, which I start with the CommandLineJobRunner . But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.