Search code examples
javaarrayliststatisticsexport-to-csv

Add a list of integers into a csv file (JAVA)


All I want is to add a list of numbers from an algorithm (which is really long so I won't show it here but gave an output of [2039, 25553, 189030, 1449869, 134295, 352258, 1588788, 718293, 353578, 1495712, 539563, 1691741, 1032543, 362844, 1143463, 4671463])

List<Integer> List1 = new ArrayList<>();

to a csv file and produce a line graph from it. I've done some searching for answers but I couldn't find anything specific for this. Any help will be much appreciated, thank you!


Solution

  • public class Try
    {
        public static void main(String[] args) throws IOException
        {
            List<Integer> List1 = new ArrayList<>();
    
            //For the example, i populate the list
            List1.add(123);
            List1.add(456);
            List1.add(789);
    
            PrintWriter outFile = new PrintWriter(new FileWriter("outNumbers.csv"));
    
            List1.stream().forEach(i->outFile.print(i + ", "));
    
            outFile.close();
        }
    }