Solved

How to create a CSV file in java

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

705 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

18 Experts available now in Live!

Get 1:1 Help Now