Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Invalid char between encapsulated token and delimiter in Apache Commons CSV library

I am getting the following error while parsing the CSV file using the Apache Commons CSV library.

Exception in thread "main" java.io.IOException: (line 2) invalid char between encapsulated token and delimiter

at org.apache.commons.csv.Lexer.parseEncapsulatedToken(Lexer.java:275)
at org.apache.commons.csv.Lexer.nextToken(Lexer.java:152)
at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:450)
at org.apache.commons.csv.CSVParser.getRecords(CSVParser.java:327)
at parse.csv.file.CSVFileParser.main(CSVFileParser.java:29)

What's the meaning of this error ?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

We ran into this issue when we had embedded quote in our data.

0,"020"1,"BS:5252525  ORDER:99999"4

Solution applied was CSVFormat csvFileFormat = CSVFormat.DEFAULT.withQuote(null);

@Cuga tip helped us to resolve. Thanks @Cuga

Full code is

    public static void main(String[] args) throws IOException {
    FileReader fileReader = null;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withQuote(null);
    String fileName = "test.csv";

    fileReader = new FileReader(fileName);
    CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat);

    List<CSVRecord> csvRecords = csvFileParser.getRecords();

    for (CSVRecord csvRecord : csvRecords) {
        System.out.println(csvRecord);
    }
    csvFileParser.close();
}

Result is

CSVRecord [comment=null, mapping=null, recordNumber=1, values=[0, "020"1, "BS:5252525  ORDER:99999"4]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...