Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create a CSV file in java

Posted on 2013-01-03
4
Medium Priority
?
764 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 546 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 549 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …
Suggested Courses

715 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