opencsv CSVReader does not read \ (backslash)

Posted on 2011-10-27
Last Modified: 2012-05-12
I am reading CSV file using the following code. I use OpenCSV library.

csvReaderList.add(new CSVReader(new FileReader(csvFileNames.get(2))));
while ((nextLineCsv1 = csvReaderList.get(0).readNext()) != null&&(nextLineCsv2= csvReaderList.get(1).readNext()) != null&&(nextLineCsv3 = csvReaderList.get(2).readNext()) != null) {
		    	ArrayList<String[]> rowFromAllThreeCsv=new ArrayList<String[]>();

Open in new window

Below you can see the exact csv file line. The first one succeeds it has \ followed by space. The next one fails it has only \. When read the first one returns an array of 12 cells and the next one has only 6.

The below works
"25-Jan-2008 08:46:00","datamirror","","50545","#PGAAGIN    ","1020360224","\                             ","0","TRSYS","CMPLACTS    ","",""

The below fails
"25-Jan-2008 08:46:00","datamirror","","50545","#PGAAGIN","1020360224","\","0","TRSYS","CMPLACTS","",""

I think i need to initilise the CSVReader wih the right constructor. Please help.
Question by:PearlJamFanatic

    Author Comment

    The problem could be with special characters (something that looks like space character). The \ appears in the line that works. Its \ followed by the special characters that didn't work.
    LVL 19

    Accepted Solution

    The default escape character for quotes (so it can appear in the data) is the backslash. In the event that you want backslash to be a valid data character there is a constructor that lets you modify which character is the escape:
    CSVReader(Reader reader, char separator, char quotechar, char escape)
    You can use CSVParser.DEFAULT_SEPARATOR and CSVParser.DEFAULT_QUOTE_CHARACTER as the values for the separator and quotechar arguments. Use whatever character you must then for the escape.

    If you're data wasn't created with the concept of an escape character then an ugly alternative would be to pre-process the input and either remove or escape the escape character.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    countClumps  challenge 10 66
    factorial example challenge 10 44
    return in catch statement 1 32
    wordlen challenge 3 26
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now