Announcement Announcement Module
No announcement yet.
How to escape \n in flat file Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to escape \n in flat file


    We process csv flat files, the line separator is \n, but certain data fields in the file can contain \n too, how can managethis situation properly with ItemReader interface ?

    Thanks a lot.

  • #2
    if you know the count of fields per line you could use something like the AggregateItemRerader (from official Spring Batch Samples)

    you do not need the header/footer concept, but rather something like a fieldCounter which decides when to set the resultHolder exhausted

    [edith says]

    I like the concept and created a running proof of concept example in my github repository, it's under "readers", look for "field-count-job.xml".

    Only custom code used is:

     * FieldCountItemReader, constructs line by reading from wrapped reader, until
     * field count is reached.
    public class FieldCountItemReader implements ItemReader<List<String>> {
        private ItemReader<FieldSet> delegate;
        private int count;
        /** {@inheritDoc} */
        public List<String> read() throws Exception {
            List<String> fields = new ArrayList<String>();
            FieldSet field = null;
            // read until end of file
            while ((field = != null) {
                String[] fieldSetValues = field.getValues();
                for (int i = 0; i < fieldSetValues.length; i++) {
                // field count reached ?
                if (fields.size() != count) {
                } else {
                    return fields;
            // reader returned nothing
            return null;
        public void setCount(int count) {
            this.count = count;
        public void setDelegate(ItemReader<FieldSet> delegate) {
            this.delegate = delegate;
    Last edited by michael.lange; Sep 25th, 2011, 05:02 PM.