Solved

How to create a CSV file in java

Posted on 2013-01-03
4
757 Views
Last Modified: 2013-01-11
Hi,
I have a requirement to create a CSV file using this open source package au.com.bytecode.opencsv.CSVWriter;

Here is what my requirement is:
1) Retrieve User information from database. I get it in the Java Bean object:- I have done this part here is the sample code
List<BusinessUser> users = usedao.getUsers();
//CSVWriter writer = new CSVWriter(new Writer("path"), "|" );
            if (users != null && users.size() > 1) {
                              Iterator<BusinessUser> it = users.iterator();
                  while (it.hasNext()){
                        BusinessUser bu = it.next();
                        // Test
                        String loginName = bu.getLoginName();
                        String firstName = bu.getFistName();
                        String lastName = bu.getLastName();
                        
                  }
                  
            }
2) public void createHeaderToCSVFile(){
            // Header name are" Login Name", "First Name" "Last Name".... there are more colums, but to make it easy for the post I have included only 3 columns
      }

3) public void writeRowToCSVFile(){
            
      }

Can someone please help me on this. I need to use | delimeter.

Thanks
0
Comment
Question by:EmaadAli
  • 2
4 Comments
 

Author Comment

by:EmaadAli
ID: 38742601
Can some one help me on this pls
0
 
LVL 1

Assisted Solution

by:KyleChamberlin
KyleChamberlin earned 182 total points
ID: 38742621
Do you absolutely have to use that package?  Writing a CSV file is quite simple, and no library should be needed.  Here is some code that will do what you need.
char del = '|';
File csv = new File("path");
FileWriter fw = new FileWriter(csv);
fw.write("Login Name" + del + "First Name" + del + "Last Name\r\n");
for (BusinessUser bu : usedao.getUsers())
	fw.write(bu.getLoginName() + del + bu.getFirstName() + del + bu.getLastName() + "\r\n");
fw.close();

Open in new window

0
 
LVL 19

Accepted Solution

by:
Thommy earned 183 total points
ID: 38742933
Check "Constructor Summary" at CSVWriter class reference...
Class CSVWriter

CSVWriter(java.io.Writer writer, char separator)
Constructs CSVWriter with supplied separator.

Supply your separator as second parameter:
w = new CSVWriter( new Writer("path"), '|'  );  
0
 

Author Closing Comment

by:EmaadAli
ID: 38768977
Thanks, both your inputs helped.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 67
Java Jpanels and Jframe 8 28
I need an assist with a programming logic math question. 5 55
map related example 6 30
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

930 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

9 Experts available now in Live!

Get 1:1 Help Now