Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

java - Unterminated Double Quotes in Spring Batch

I am new to Spring Batch and I have run into a problem.

The batch application I am working on reads and processes lines from a delimited text file. I have configured the application to use a FlatFileReader to read the delimited text file, but the issue is that some of the data being read has a double quote in it. A FlatFileParseException is thrown when the FlatFileReader encounters a single double quote, but none is thrown when two double quotes are present.

Has anyone come across this issue, and if so, what would be the proper resolution? Manipulating the data itself is not an option unfortunately. I have tried adding an escape character before every double quote, but an exception is still thrown regardless.

Any help would be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ran into the same problem. However the proposed solution is not an optimal one. What if in your data there isn't a suitable quote character? Unfortunately we don't always have control over input data and pre-processing them is not often a good idea. Exploring the DelimitedLineTokenizer source code I decided to adopt this solution that I will share with this answer. It requires to override a class, but with this we totally remove the quote character issue.

import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;

    public class CustomDelimitedLineTokenizer extends DelimitedLineTokenizer {

        @Override
        protected boolean isQuoteCharacter(char c) {
            return false;
        }

    } 

This way the DelimitedLineTokenizer can't recognize the quote character. Of course if we need this functionality then this solution is not adoptable, however I think it is better than the proposed one that just sort the issue instead of solving it. Hope it will help someone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...