Como escrever uma lista de ArrayLists para o formato CSV em Java
Eu tenho dados no formato abaixo:
List<ArrayList<String>>
Eu quero escrever para CSV. Abaixo está o meu código:
private static void writeToCSV(List<ArrayList<String>> csvInput) throws IOException{
String csv = "C:\\output.csv";
CSVWriter writer = null;
try {
writer = new CSVWriter(new FileWriter(csv));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(ArrayList<String> each: csvInput){
writer.writeNext(each);
}
writer.close();
}//end of writeTOCSV
O método 'writeNext' permite apenas String [] como argumento. Quando tento digitar cast 'ArrayList each' em String [] usando um Object [] como mostrado abaixo, estou recebendo um erro de conversão de tipo em tempo de execução:
Object[] eachTemp = each.toArray();
writer.writeNext((String[]) eachTemp);
Alguém poderia me dizer onde estou errado?