0

I have List<ArrayList<String>> objects and i want to write them into csv file column by column in java.

List<ArrayList<String>> contains 

[[fileName, abc.sldprt, pqr.sldprt, jku.sldprt, cmx.sldprt, abc.sldprt],
[cadName, abc.sldprt, pqr.sldprt, jku.sldprt, cmx.sldprt, abc.sldprt],
[number, abc.sldprt, pqr.sldprt, jku.sldprt, cmx.sldprt, abc.sldprt],
[folder_Id, 098123, 098124, 094123, 0981290, 09812354],
[Directory_Path, 098123, 098124, 094123, 0981290, 09812354],
[wC_State, Working, WIP, Released, Development, Working],
[legacy_Path, C:\WC, C:\WC, C:\WC, C:\WC, C:\WC],
[reverified, Empty, Empty, Empty, Yes, Empty],
[createdBy, 12132019, 12092019, 12072019, 12132018, 12132019],
[originDate, 12132019, 12092019, 12072019, 12132018, 12132019],
[iteration, 2, 8, 13, 19, 3],
[shouldImport, True, True, True, True, True],
[Mymap, True, True, True, True, True],
[YourMap, True, True, True, True, True]]

Till now i have tried below code

String csv = "C:\\Users\\Output.csv";
        CSVWriter writer = null;
        try {
            writer = new CSVWriter(new FileWriter(csv),'|',CSVWriter.NO_QUOTE_CHARACTER,
                    CSVWriter.DEFAULT_ESCAPE_CHARACTER,
                    CSVWriter.DEFAULT_LINE_END);
            for(ArrayList<String> each: databaseColListHeaderChange){
                writer.writeNext(each.toArray(new String[each.size()]));
            }
            writer.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I'm getting the data row wise in the csv file. is there any way to write ArrayList inside list column wise into csv file

Right now i get the csv file in following format

fileName|abc.sldprt|pqr.sldprt|jku.sldprt|cmx.sldprt|abc.sldprt
cadName|abc.sldprt|pqr.sldprt|jku.sldprt|cmx.sldprt|abc.sldprt
number|abc.sldprt|pqr.sldprt|jku.sldprt|cmx.sldprt|abc.sldprt
folder_Id|098123|098124|094123|0981290|09812354
Directory_Path|098123|098124|094123|0981290|09812354
wC_State|Working|WIP|Released|Development|Working
legacy_Path|C:\WC|C:\WC|C:\WC|C:\WC|C:\WC
reverified|Empty|Empty|Empty|Yes|Empty
createdBy|12132019|12092019|12072019|12132018|12132019
originDate|12132019|12092019|12072019|12132018|12132019
iteration|2|8|13|19|3
shouldImport|True|True|True|True|True
Mymap|True|True|True|True|True
YourMap|True|True|True|True|True

I need the data in following format

fileName|cadName|number|folder_Id|Directory_Path
abc.sldprt|abc.sldprt|abc.sldprt|098123|098123
pqr.sldprt|pqr.sldprt|pqr.sldprt|098124|098124
jku.sldprt|jku.sldprt|jku.sldprt|094123|094123
cmx.sldprt|cmx.sldprt|cmx.sldprt|0981290|0981290
abc.sldprt|abc.sldprt|abc.sldprt|09812354|09812354
...so on all the list

please if any suggestion or help?

Knash
  • 7
  • 2
  • Closely related: https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array – slim Apr 23 '19 at 15:11

2 Answers2

0

You need to normalize your data into a map where key will be your first arrayList entry and value will be the rest of the list.

{
 fileName=[abc.sldprt, pqr.sldprt, jku.sldprt, cmx.sldprt, abc.sldprt],
 cadName=[abc.sldprt, pqr.sldprt, jku.sldprt, cmx.sldprt, abc.sldprt],
...
}

and then use the map keys as headers

ACV
  • 9,964
  • 5
  • 76
  • 81
  • i did normalize the data Map> ,here i wanted to know how to use map keys as header or is there any peace of code for reference? – Knash Apr 23 '19 at 15:50
0

You just need to think through the steps that you would take if you were writing out the content of this array of arrays by hand.

The bare bones of this, java-flavoured pseudocode:

for(int column=0; column<numColumns; column++) {
   for(int row=0; row<numRows; row++) {
      output content[row][column];
      output "|" when necessary
   }
   output newline
}

The key difference between this and what you've already done is that you're flipping the order of content[column][row] to content[row][column].

Note that if this isn't an academic exercise, you should use a library such as Commons CSV to read/write CSV-like data, because there can be subtle issues, and they've been solved.

slim
  • 40,215
  • 13
  • 94
  • 127