0

I need to parse a csv file and store the data into a java bean class. Like:

email,fname,lname
[email protected],abc,xyz

These details into a bean class. I am able to parse a csv file. How to store details in bean class.

private static List<List<String>> readTXTFile(String csvFileName) throws IOException {

     String line = null;
    BufferedReader stream = null;
    List<List<String>> csvData = new ArrayList<List<String>>();

    try {
        stream = new BufferedReader(new FileReader(csvFileName));
        while ((line = stream.readLine()) != null) {
            String[] splitted = line.split(",");
            List<String> dataLine = new ArrayList<String>(splitted.length);
            for (String data : splitted)
                dataLine.add(data);
            csvData.add(dataLine);
        }
    } finally {
        if (stream != null)
            stream.close();
    }

    return csvData;

}

the above code is parsing csv file but i want that data to be inserted in java bean class.

2
  • Instead adding the elements to a List (dataLine) add them to the bean and then add the bean to csvData (changing the generic from List<String> to your bean class) Commented May 20, 2014 at 9:28
  • I don't see your bean class code, so how should we know how to help? Commented May 20, 2014 at 9:32

2 Answers 2

1

Create a POJO

private class Data{//You can make it public too, in a separate class
    String email;
    String fname;
    String lname;

    //Getters

    //Setters
}

In your original class you can use like this

List<Data> datas = new ArrayList<Data>();
try {
    stream = new BufferedReader(new FileReader(csvFileName));
    while ((line = stream.readLine()) != null) {
        String[] splitted = line.split(",");
        Data data = new Data();
        data.setEmail(splitted[0]);
        data.setFname(splitted[1]);
        data.setLname(splitted[2]);

        datas.add(data);
    }
} 

You will have all rows in your arraylist. You can put a null check to make it more efficient.

Sign up to request clarification or add additional context in comments.

Comments

1

Use a OpenCSV

Writing

        CSVWriter writer = new CSVWriter(new FileWriter("myfile.csv"), ',');
        String[] headers = {"id","name","address"};
        writer.writeNext(headers);

Reading(The example of setting inside the bean and add to list)

USER_ID_INDEX etc is constant

    CSVReader reader = new CSVReader(new InputStreamReader(userManActionForm.getUploadFile().getInputStream()));
    List<String[]> entries = reader.readAll();
    List<Member> memberList = new ArrayList<Member>();
        for (String[] entry : entries) {
            Member member = new Member();
            member.setUserId(entry[USER_ID_INDEX]);
            member.setUserName(entry[USER_NAME_INDEX]);
            member.setGroup(entry[USER_GROUP_INDEX]);
            memberList.add(member);
        }

https://code.google.com/p/opencsv/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.