Create CSV file in Java Example Code File Options

In the previous post, we explained how to create a CSV file in Java by extracting rows from Oracle table. We presented a basic Java program that generated the CSV file with standard "," as separator, optionally enclosed by quotes. Ever since I wrote that post, I have been getting questions from my readers on how to specify a different separator, different option to enclose individual columns etc. I have collated all of them and providing a solution for them in this post now.

Note that you need to take the same program from the previous post and replace the content as required in this post. I have made this post as a Q&A type so that it can easily suit to your needs and be specific to the question.

1) Instead of using a comma separated value, I would like to use a PIPE delimited set of values. Is it possible to specify a different delimiter when producing the file?

Yes, you can specify a different/ any delimiter. To do this, you need to modify the following line of the Java code.

18           char separator='|';
19           CSVWriter my_csv_output=new CSVWriter(my_csv,separator); //Create a writer object

This example produces a Pipe (|) delimited CSV file which you can see from the screenshot below:

Pipe delimited CSV output in Java
Pipe delimited CSV output in Java
2) I have a requirement to use percentage instead of double quotes as optional quotes candidate in my project. How can I specify this in Java when creating the output file?

Yes, you can specify a different quote character also. To do this, you have to make the following changes in the code.
18           char separator='|';
19           char quotechar='%';
20           CSVWriter my_csv_output=new CSVWriter(my_csv,separator,quotechar); //Create a writer object

This example produces a CSV file with pipe delimiter and '%' as separator. A sample CSV file output is provided below:

CSV file generation in Java
percentage as quote character

3) I would not like my column headers to be visible when generating the CSV file. How can this be done in this approach?

If you want to suppress the column names, you have to set the boolean includecolumnnames to false as shown below

21           boolean includecolumnnames=false;
22           my_csv_output.writeAll(query_set,includecolumnnames); //write output by directly reading the Resultset, include column names in report

This will remove the column header names in the output.If you want to have a logical / meaningful column name for your CSV data, you need to use the "AS" alias in your SQL string suitably.

4)How to specify a different line ending character in the program?

Assume that the line ending character needs to be "@" instead of the standard return. You can do this as per below;
18           char separator='|';
19           char quotechar='%';
20           String lineend="@";
21           CSVWriter my_csv_output=new CSVWriter(my_csv,separator,quotechar,lineend); //Create a writer object

5) Some of my column data in the table has a white space in the data. Is there a flexibility in the program to TRIM the whitespace in the output file? What are the options to remove whitespace in the generated file?

If you want to trim the whitespace in column value you can do this in the SQL directly by using the TRIM command. If SQL does not suit to your needs, you can do this in Java also while creating the file. The "writeall" method takes a boolean variable optionally that specifies whether TRIM needs to be done or not. Give it a try and let us know how it goes.

Do you have a FAQ on the CSV generation? Or a challenging situation that you want us to help with? Head right down to the comments section and wait for a spontaneous response from our friendly team.

No comments:

Post a Comment