Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

convert arraylist to csv file java

import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
class test
{
    public static void main(String arg[])throws IOException
    {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("2212");
        list1.add("gmail.com");

        ArrayList<String> list2 = new ArrayList<>();
        list2.add("192.168.0.175");
        list2.add("403");
        list2.add("192.168.0.26");
        list2.add("1809");

        File file = new File("test.csv");
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("Domain,Total");
        bw.newLine();
        for(int i=0;i<list1.size();i++)
        {
            bw.write(list1.get(i+1)+","+list1.get(i++));
            bw.newLine();
        }
        bw.write("
Application,Total");
        bw.newLine();
        for(int i=0;i<list2.size();i++)
        {
            bw.write(list2.get(i++)+","+list2.get(i));
            bw.newLine();
        }
        bw.close();
        fw.close();
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #convert #arraylist #csv #file #java
ADD COMMENT
Topic
Name
4+7 =