Home » Java » Writing/appending data to a CSV file, column wise, in JAVA-Exceptionshub

Writing/appending data to a CSV file, column wise, in JAVA-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to write/append data to a CSV file, column-by-column, in below fashion:

    query1       query2       query3
    data_item1   data_item7   data_item12
    data_item2   data_item8   data_item13
    data_item3   data_item9   data_item14
    data_item4   data_item10 
    data_item5   data_item11
    data_item6  

I have the data in a hashMap, with the queryID (i.e. query1,query2) being the key and data_items for the
corresponding queries being the values.
The values(data_items for every query) are in a list.
Therefore, my hash map looks like this :

HashMap<String,List<String>> hm = new HashMap<String,List<String>>();

How can I write this data, column by column to a csv, as demonstrated above, using JAVA ?
I tried CSVWriter, but couldn’t do it. Can anyone please help me out ?

How to&Answers:

csv files are mostly used to persist data structured like a table… meaning data with columns and rows that are in a close context.

In your example there seems to be only a very loose connection between query1, 2 and 3, and no connection horizontally between item 1,7 and 12, or 2, 8 and 13 and so on.

On top of that writing into files are usually facilitated along rows or lines. So you open your file write one line, and then another and so on.

So to write the data columnwise as you are asking, you have to either restructure your data in your code alrady to have all the data which is written into one line available on writing that line, or run through your csv file and it’s lines several times, each time adding another item to a row. Of course the latter option is very time consuming and would not make much sense.

So i would suggest if there is really no connection between the data of the 3 queries, you either write your data into 3 different csv files: query1.csv, 2.csv and 3.csv.

Or, if you have a horizontal connection i.e. between item 1,7 and 12, and so on you write it into one csv file, organizing the data into rows and columns. Something like:

queryNo     columnX     columnY     columnZ
1           item1       item2       item3
2           item7       item8       item9
3           item12      item13      item14 

How to do that is well described in this thread: Java – Writing strings to a CSV file.
Other examples you can also find here https://mkyong.com/java/how-to-export-data-to-csv-file-java/