Solved

How to create a CSV file in java

Posted on 2013-01-03
4
758 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries email authority 6 57
Need help making a program or form for inventory that exports to txt 8 41
jsp login check 12 32
Which non-HTML GUI front end to use with Java? 3 22
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 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