Solved

How to create a CSV file in java

Posted on 2013-01-03
4
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

752 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