• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Parse CSV using runtime configurable bean class in Java  RSS feed

kalees waran
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will detailedly explain my requirement below,. I am going to automate a manual process. I will be reading multiple CSV files from a remote location using java.

There are five formats of input files are expected, each differs in their header structure. For example, Type 1 - Number, ID, Name, Phone, Address

Type 2 - Number, GID, Employee Name, Address1, Address2, Phone number

and so the other three types are also differs.

The precondition is not all the files are expected for a particular run. I need to read these files one by one, validate it, log the validation error and i have to consolidate all the correct data from all the files together in a standard output format, in a single file The standard output format will be like,

Number, Name, ID, Address

I need to have the above data alone in the output file and rest of the data can be ignored.

What i have tried is as follows, I have created 5 bean classes representing each type's header. I just read an input, identify its type and parsed it. I parsed line by line.
public String[] parseCSV(String inputLine){
try {
String[] fields;

Pattern p =
fields = p.split(inputLine);
/*for ( int i = 0; i < fields.length; i++ ) {

return fields;


I have validated as per the validation rules and i appended each line elements into an object. I have added all the objects in to a MAP collection. Likewise, i have created 5 beans and did the same.

But, what is the change needed now is,. All the headers in all the five types of rosters are configurable items. hence, i have to change my bean classes everytime when the header structures are changed.

We have to create one single utility, which is configurable for all the five types of input files. To be very clear, if type 1 input comes with 8 columns and type 3 comes with 12 columns, the utility is able to parse it.

We are going to have a table which has the data regarding the header structure of all the five types of inputs alone. Once i read a file and identify its type, i will hit the database and read the header structure of that particular type and its column count. I will match the column count with the input file's header count and i will have to proceed creating a bean class on runtime depending upon the header structure read now. I will validate and consolidate as i did above.

The requirement is, Runtime configuration of bean class, depending upon the type of input

Please let me know your idea and i will appreciate if you share the code also.
Ulf Dittmer
Posts: 42972
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the format of the CSV isn't known until runtime, and can frequently change, then it doesn't seem a good idea to me to want to have individual classes for each type. Why is that a requirement?

And, of course, I always advise not to write your own CSV parser, but to use one of the numerous existing libraries: http://www.coderanch.com/how-to/java/AccessingFileFormats
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!