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

  • How to escape \n in flat file

    Hi,

    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.
    Regards

  • #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:

    Code:
    /**
     * 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} */
        @Override
        public List<String> read() throws Exception {
            List<String> fields = new ArrayList<String>();
            FieldSet field = null;
            // read until end of file
            while ((field = delegate.read()) != null) {
                String[] fieldSetValues = field.getValues();
                for (int i = 0; i < fieldSetValues.length; i++) {
                    fields.add(field.getValues()[i]);
                }
                // field count reached ?
                if (fields.size() != count) {
                    continue;
                } 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.

    Comment

    Working...
    X