简体   繁体   中英

Auto generate Java Classes From a csv file

I'm trying to generate Java POJO classes automatically from csv files where the first line contains headers and the other ones contain data. I found many examples and tools in the net showing how to parse csv files and put data into Java objects but in all of them Java POJO classes have already been created manually and I'm looking for a tool which generates them automatically. Otherwise I need a tool similar to Hibernate reverse engineering Tools but instead of databases I've csv files.

For example here is my input csv file:

username, password,   date,        zip,  town

Klaus,    qwexyKiks,  17/1/2007,1111, New York

Oufu1,     bobilops,    10/10/2007,4555, New York

And here is what I need to generate automatically:

package test;

import java.util.Date;

public class UserBean {
    String username;
    String password;
    Date date;
    int zip;
    String town;


    public Date getDate() {
        return date;
    }
    public String getPassword() {
        return password;
    }
    public String getTown() {
        return town;
    }
    public String getUsername() {
        return username;
    }
    public int getZip() {
        return zip;
    }
    public void setDate(final Date date) {
        this.date = date;
    }
    public void setPassword(final String password) {
        this.password = password;
    }

    public void setTown(final String town) {
        this.town = town;
    }
    public void setUsername(final String username) {
        this.username = username;
    }
    public void setZip(final int zip) {
        this.zip = zip;
    }
}

I think that what you want to do would be quite slow and not fault-tolerant.

The main difference between tables in relational database management systems and csv files is that csv files do not have data type information . So the software that you would like to have shoud apply type inference on columns and that could be very slow and error prone. If you are going to parse big files that may contain errors, you wouldn't have a validation functionality, and execution tyme could be a bottleneck to the whole system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM