Questions tagged [opencsv]

opencsv is a simple csv (comma-separated values) parser library for Java

What is opencsv ?

opencsv is a simple csv (comma-separated values) parser library for Java. It was developed in response to a lack of csv parsers with commercial-friendly licenses.

Where can I get it?

Source and binaries are available from Sourceforge. You can check out the javadocs online.

What features does opencsv support?

opencsv supports all the basic csv-type things you're likely to want to do:

  • Arbitrary numbers of values per line
  • Ignoring commas in quoted elements
  • Handling quoted entries with embedded carriage returns (ie entries that span multiple lines)
  • Configurable separator and quote characters (or use sensible defaults)
  • Read all the entries at once, or use an Iterator style model
  • Creating csv files from String[] (ie. automatic escaping of embedded quote chars)
1008 questions
71
votes
22 answers

OpenCSV: How to create CSV file from POJO with custom column headers and custom column positions?

I have created a MappingsBean class where all the columns of the CSV file are specified. Next I parse XML files and create a list of mappingbeans. Then I write that data into CSV file as report. I am using following annotations: public class…
Vikram Pathania
  • 873
  • 2
  • 8
  • 15
39
votes
3 answers

setting a UTF-8 in java and csv file

I am using this code for add Persian words to a csv file via OpenCSV: String[] entries="\u0645 \u062E\u062F\u0627".split("#"); try{ CSVWriter writer=new CSVWriter(new OutputStreamWriter(new FileOutputStream("C:\\test.csv"), "UTF-8")); …
mehdi
  • 686
  • 2
  • 9
  • 21
38
votes
8 answers

Unwanted double quotes in generated CSV file

I have created a CSV file using the Java code below: String csv = rs.getString("UPLOAD_FOLDER_PATH")+".csv"; CSVWriter writer = new CSVWriter(new FileWriter(csv)); String [] filevalues = new…
Edward
  • 1,367
  • 8
  • 26
  • 43
30
votes
10 answers

OpenCSV - How to map selected columns to Java Bean regardless of order?

I have a CSV file with the following columns: id, fname, telephone, lname, address. I have a Person class with id, fname and lname data members. I want to map only these columns to Person object from a CSV file and discard telephone and address…
jsf
  • 2,851
  • 9
  • 30
  • 33
24
votes
6 answers

Skip first line using Open CSV reader

Here is the line i am using currently File booleanTopicFile; // booleanTopicFile is csv file uploaded from form CSVReader csvReader = new CSVReader(new InputStreamReader(new FileInputStream(booleanTopicFile), "UTF-8")); Want to skip the first line…
Sangram Anand
  • 10,526
  • 23
  • 70
  • 103
20
votes
4 answers

Why opencsv capitalizing csv headers while writing to file

While writing Beans to CSV file by using OpenCSV 4.6, all the headers are changing to uppercase. Eventhough bean has @CsvBindByName annotation it is changing to uppercase. Java Bean: public class ProjectInfo implements Serializable { …
gprasadr8
  • 789
  • 1
  • 8
  • 12
20
votes
6 answers

StatefulBeanToCsv with Column headers

I am using opencsv-4.0 to write a csv file and I need to add column headers in output file. Here is my code. public static void buildProductCsv(final List product, final String filePath) { try { Writer writer = new…
Bishan
  • 15,211
  • 52
  • 164
  • 258
17
votes
1 answer

Parse CSV file containing a Unicode character using OpenCSV

I'm trying to parse a .csv file with OpenCSV in NetBeans 6.0.1. My file contains some Unicode character. When I write it in output the character appears in other form, like (HJ1'-E/;). When when I open this file in Notepad, it looks ok. The code…
meysam_pro
  • 225
  • 1
  • 2
  • 14
16
votes
5 answers

opencsv in java ignores backslash in a field value

I am reading a csv file using opencsv. I am ignoring the first line of; the csv file is tab separated with some values enclosed in double quotes. The problem occurs when I read the values of a column that has the '\' character, this is stripped out…
ahaneo
  • 187
  • 1
  • 1
  • 4
16
votes
4 answers

Writing at the end of a file via opencsv

I'm using opencsv and want to write to a .csv file through multiple sessions. However every time I start a new CSVWriter the old file gets erased. Can I change the behavior of the CSVWriter to write at the end of the file instead of replacing the…
Christian
  • 25,249
  • 40
  • 134
  • 225
16
votes
1 answer

How to export data to csv file in Android?

I created a csv file with the following format which I'm aiming to output to the device's sd card: Ship Name,Scientist Name,Scientist Email,Sample Volume,Sample Colour,Longitude,Latitude,Material,Date Each of the vales in the csv will be of type…
Brian Var
  • 6,029
  • 25
  • 114
  • 212
15
votes
4 answers

Good and effective CSV/TSV Reader for Java

I am trying to read big CSV and TSV (tab-separated) Files with about 1000000 rows or more. Now I tried to read a TSV containing ~2500000 lines with opencsv, but it throws me an java.lang.NullPointerException. It works with smaller TSV Files with…
Robin
  • 3,512
  • 10
  • 39
  • 73
15
votes
2 answers

opencsv CSVWriter using utf-8 doesn't seem to work for multiple languages

I have a very annoying encoding problem using opencsv. When I export a csv file, I set character type as 'UTF-8'. CSVWriter writer = new CSVWriter(new OutputStreamWriter("D:/test.csv", "UTF-8")); but when I open the csv file with Microsoft Office…
user1213162
  • 185
  • 1
  • 1
  • 5
14
votes
4 answers

Trim leading and trailing spaces in OpenCSV

I am using OpenCSV's CSVReader to read some comma separated values from a file. I'm not sure how to trim leading and trailing spaces. Sure, I could do String.trim() but it would be cleaner not to. In the documentation there is no such option…
user1377000
  • 1,433
  • 3
  • 17
  • 29