Menu

FAQ

Scott Conway

What was the big change between version 2.3 and 3.0?

Really not much. The reason I changed the major version number was because we moved from java 5 to java 7. This meant that anyone still using java 5 or java 6 could not upgrade - which to me denotes a major point increment.

Starting with version 3.0 I have kept the change list on this wiki. If you have any questions about past or planned changes since 3.0 please check [What's new] page.

Foreign characters are showing up as blocks or question marks?

I have had several questions about foreign character sets and found several good articles on stack overflow about dealing with them but unfortunately I have never received feedback on if it worked or not. Here are the links I found if they do help or not please let me know.

https://stackoverflow.com/questions/4964640/reading-inputstream-as-utf-8

http://stackoverflow.com/questions/696626/java-filereader-encoding-issue

http://stackoverflow.com/questions/11868022/unicode-characters-appearing-as-question-marks-in-java-json-parsing.

https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding/362006#362006

How do I escape quotation character?

Here is where opencsv configuration is both a blessing and a curse. When creating a CSVReader, Writer, Parser you can define what the quotation mark and the escape character is (otherwise they default). This is why I will always try and say quotation character instead of quotation mark because you define what opencsv treats as a quotation mark. That said there are two ways to escape a quotation character:

  • With the defined escape character.
  • With the defined quotation character. So if you create a CSVParser with a percent sign (%) as a quote mark and a at symbol (@) as an escape symbol then either @% or %% would be considered an escaped quotation character.

Getting NoClassDefFoundError when using Android

If you see simular to the following:

05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pk.opencsvpoc, PID: 25829
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;

It is because Android only ported over a subset of java.beans into its version of java. You can see a list of what is support at https://developer.android.com/reference/java/beans/package-summary

There are plans to switch over to reflection in opencsv 5.0 but I doubt it will remove all our dependencies on java.beans. For now the best suggestion for opencsv is to steer clear of the com.opencsv.bean classes until Android fully supports java.beans or we are successful in removing java.beans.

Another possibility is to try another csv library. I checked apache commons csv and super-csv and apache does not convert to beans but super-csv does using only reflection.

I am getting an error when using the CSVParser

Unless you are dealing with custom csv files my default recommendation is to switch over to the RFC4180Parser. The CSVParser was the original parser for opencsv and predates the RFC4180 specification. It is made for a high degree of customization but there exist data that it, even with all its customizations, it cannot parse. If the data does follow the RFC4180 specification then use the RFC4180Parser - it will be a lot easier to set up and parse.


Related

Wiki: Home
Wiki: What's new

Discussion

  • MABETH BORRES

    MABETH BORRES - 2020-05-22

    Hi,
    Is there a maximum number of columns that opencsv can create? I am supposed to create a csv file with 1,116 columns, but what got generated was up to 1,000 columns only in supercsv.

    I just would like to  understand the behavior in opencsv if there's a difference from supercsv.
    Thanks.

     
    • Andrew Rucker Jones

      As many columns as Java can fit in an integer. I think your 1,116 columns are safe.

       
  • Juan Manuel Buendia

    Good morning,
    I want to use CSVWriter or CSVParserWriter to output the file in .csv format (as opposed a .txt, etc). When I look at the .csv file, I want the file to have leading 0s (no converstion of any of my ctring arrays to number. So if I output the following:
    String[] array = {"0000001", "02345698", "120", "3" , "10"};
    I should see the following cells in my .csv file:
    0000001 02345698 120 3 10
    currenlty, using writer, I get the following:
    1 2345698 120 3 10

    Please help, thank you

     
    • Andrew Rucker Jones

      Would you please open a regular ticket for this and include:
      1. Code, and
      2. Data?

       
  • Sanjay

    Sanjay - 2022-06-01

    Hi,
    CSVWriter is not writing all the data from the resultset, It is breaking in between. In my resultSet have 4000 record but the csv file is generated with 1401 record, it is breaking in between.
    This is intimetent issue, some time it writes whole data and some time its break in between.
    I am using StreamingResponseBody with below code:

    private double writeToOutputStreamUsingCSVWriter(OutputStream outputStream, ResultSet resultSet,
    DownloadMaster downloadMaster) {
    double totalBytes = 0;

    if (Objects.nonNull(outputStream) && Objects.nonNull(resultSet)) {
    try (CountingOutputStream counterStream = new CountingOutputStream(outputStream);
    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(counterStream));
    CSVWriter csvWriter = new CSVWriter(bufferedWriter)) {

        int csvTotalRecord = csvWriter.writeAll(resultSet, true);
    
        log.info("ResultSet writing is completed, csvTotalRecord are : {}, request key  : {}",
            csvTotalRecord, downloadMaster.getId());
    
      totalBytes = counterStream.getByteCount();
    
    } catch (ClientAbortException | NullPointerException e) {
      log.error("Client abort error while writing to output stream : {}", downloadMaster.getId(), e);
    } catch (Exception e) {
      log.error("Error while writing to output stream : {}", downloadMaster.getId(), e);
    }
    

    }
    return totalBytes;
    }

    Could you please help me.
    Thanks.

     

    Last edit: Sanjay 2022-06-01
    • Scott Conway

      Scott Conway - 2022-06-06

      The only thing I can think of offhand is to have a finally statment in the try catch where you flush the writer.

      finally {
      csvWriter.flush();
      }

      though in you code you should probably put it between the writeAll and the getByteCount to force the count to be correct..

       

Log in to post a comment.