2

I need to write my data inside two arraylist into these format of CSV file

Domain         Total
gmail.com      90

Application     Total
IP 1             30
IP 2             30
IP 3             30

the first arraylist will store the data for domain and total which will look like the following. The sequence of the data in the arraylist is Total,Domain

[2212, gmail.com"]

the second arraylist will store the data for application and total which will look like the following.The sequence of the data in the arraylist is IP,Total

["192.168.0.175", 403, "192.168.0.26", 1809]

I am looking a best way for write my data into the csv file but I cant really understand how it's work? Can anybody provide any direction, I am lost in this part now

6
  • 1
    what have you tried so far? Are you using csvWriter? Commented Oct 12, 2017 at 5:38
  • Missing a quote mark with gmail.com example? Commented Oct 12, 2017 at 5:39
  • I tried read the example online but I am getting lost now Commented Oct 12, 2017 at 5:39
  • csvWriter will be a good option ? Commented Oct 12, 2017 at 5:40
  • Are you trying to write both kinds of data into same file? Why are you asking about two scenarios? Have you searched Stack Overflow? Many Java + CSV Questions have already been asked and answered. Explain how your Question is different and not already covered. Commented Oct 12, 2017 at 5:42

1 Answer 1

3

You don't need to use any external library for writing into CSV file. They are just txt file separated by comma.

Here is the code which will do the job :

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("\nApplication,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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

While not necessary, a library such as Apache Commons CSV does make it less of a chore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.