Suppose I have ArrayList with following contents
public void add() {
obj.add(1);
obj.add(123456789);
obj.add(1.222);
obj.add(2.333);
obj.add(2);
obj.add(9876541);
obj.add(4.555);
obj.add(7.888);
}
I want the contents of the ArrayList to be written in the txt file as follows
1,123456789,1.222,2.333
2,9876541,4.555,7,888
I have following code to write the contents to the file
public void createCSV() {
try {
FileWriter fos = new FileWriter(
"/home/data.txt");
PrintWriter out = new PrintWriter(fos);
for (int i = 0; i < obj.size(); i++) {
out.write(String.valueOf(obj.get(i)));
out.write(",");
}
out.close();
} catch (Exception e) {
}
}
public static void main(String[] args) {
JavaOp_CSV x = new JavaOp_CSV();
x.add();
x.createCSV();
}
As of now the contents in the file is
1,123456789,1.222,2.333,
Can anybody provide me with a logic to get the o/p as mention above?? Thanks in advance
1and2are "rows" in the output.