I have a CSV file which I read from and store its contents as a String Array.
I want to then write that input back to a separate CSV file, but I want to loop through and write the contents back 5 times to that CSV file.
The following code works when it prints out the code, but I assume it is overwriting the file each time when it writes to the CSV file, because it only contains 1 iteration of the loop in the CSV file?
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
String[] nextLine = null;
for (int i = 0; i<=5; i++){
CSVReader reader = new CSVReader(new FileReader("D:\\input.csv"), ',' , '"' , 1);
CSVWriter writer = new CSVWriter(new FileWriter("D:\\output.csv"));
while ((nextLine = reader.readNext()) != null) {
//Write the record to Result file
writer.writeNext(nextLine);
System.out.println(Arrays.toString(nextLine));
}
i++;
writer.flush();
writer.close();
}
Edit: the format I require is [a,b,c,a,b,c,a,b,c...] as opposed to [a,a,a,b,b,b,c,c,c...] so let's say my input file is [item1 dog cat item2 dog cat item3 dog cat] then I would expect it to print like 5 times in sequential order and not like [item 1, item 1, item 1, item 1, item 1, dog, dog...]
CSVReaderandCSVWriteroutside theforloop...