convert arraylist to csv file java

Solutions on MaxInterview for convert arraylist to csv file java by the best coders in the world

showing results for - "convert arraylist to csv file java"
Gianluca
18 May 2018
1import java.util.ArrayList;
2import java.io.BufferedWriter;
3import java.io.FileWriter;
4import java.io.File;
5import java.io.IOException;
6class test
7{
8    public static void main(String arg[])throws IOException
9    {
10        ArrayList<String> list1 = new ArrayList<>();
11        list1.add("2212");
12        list1.add("gmail.com");
13
14        ArrayList<String> list2 = new ArrayList<>();
15        list2.add("192.168.0.175");
16        list2.add("403");
17        list2.add("192.168.0.26");
18        list2.add("1809");
19
20        File file = new File("test.csv");
21        FileWriter fw = new FileWriter(file);
22        BufferedWriter bw = new BufferedWriter(fw);
23
24        bw.write("Domain,Total");
25        bw.newLine();
26        for(int i=0;i<list1.size();i++)
27        {
28            bw.write(list1.get(i+1)+","+list1.get(i++));
29            bw.newLine();
30        }
31        bw.write("\nApplication,Total");
32        bw.newLine();
33        for(int i=0;i<list2.size();i++)
34        {
35            bw.write(list2.get(i++)+","+list2.get(i));
36            bw.newLine();
37        }
38        bw.close();
39        fw.close();
40    }
41}
42