How to merge CSV files in Java

后端 未结 3 890
野性不改
野性不改 2021-01-06 11:06

My first csv file looks like this with header included(header is included only at the top not after every entry):

NAME,SURNAME,AGE
Fred,Krueger,Unknown
....          


        
3条回答
  •  花落未央
    2021-01-06 11:22

    Read in the header of the first file and create a list of the column names. Now read the header of the second file and add any column names that don't exist already in the list to the end of the list. Now you have your columns in the order that you want and you can write this to the new file first.

    Next I would parse each file and for each row I would create a Map of column name to value. Once the row is parsed you could then iterate over the new list of column names and pull the values from the map and write them immediately to the new file. If the value is null don't print anything (just a comma, if required).

    There might be more efficient solutions available, but I think this meets the requirements you set out.

提交回复
热议问题