Search code examples
javatablesaw

#Tablesaw #Java When exported to csv, the table is reduce to only the last element of the list


I self learn Java, and I am trying to do some programs for work. As in the title, the Table looks fine. But when I export to Csv only the last element is saved.

I must have made a mistake somewhere. Your help will be strongly appreciated.

    public void listInsideFolder(String nomFile) throws Exception {
    File file = new File(nomFile);
    File[] listOfFiles = file.listFiles();

    List<String> Adresses = new ArrayList<>();
    List<String> Names = new ArrayList<>();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            Adresses.add(listOfFiles[i].getAbsolutePath());
            Names.add(listOfFiles[i].getName());
            }

        StringColumn column0 = StringColumn.create("Names", Names);
        StringColumn column1 = StringColumn.create("Addresses",Adresses);

        Table t = Table.create();

        t.addColumns(column0);
        t.addColumns(column1);

      // System.out.println(t);
       // System.out.println(column0);
        // System.out.println(Names);

        t.write().csv("C:\\Users\\user\\Desktop\\Dossier\\Floweriest.csv");

        Adresses.clear();
        Names.clear();
        t.clear();
        column0.clear();
        column1.clear();
    }
}

Solution

  • Your CSV file is overwritten in each iteration as @fantaghirocco suggests.

    It's not entirely clear to me what you're trying to do. If you want a separate output file for each iteration, you need to provide a new file name for each iteration in the loop.

    If you're trying to make one output CSV file with the data from all the other files, you need to setup the table and so on, outside the loop. Like this:

    public void listInsideFolder(String nomFile) throws Exception {
    
        File file = new File(nomFile);
        File[] listOfFiles = file.listFiles();
    
        List<String> Adresses = new ArrayList<>();
        List<String> Names = new ArrayList<>();
    
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                Adresses.add(listOfFiles[i].getAbsolutePath());
                Names.add(listOfFiles[i].getName());
            }
        }
    
        StringColumn column0 = StringColumn.create("Names", Names);
        StringColumn column1 = StringColumn.create("Addresses",Adresses);
    
        Table t = Table.create();
    
        t.addColumns(column0);
        t.addColumns(column1);
    
        t.write().csv("C:\\Users\\user\\Desktop\\Dossier\\Floweriest.csv");
    }
    

    If this is what you're looking to do, you could also skip the lists, create the table before the loop, and use the .append() method to add the results directly to the column.

    Finally, a couple minor points. Adresses is spelt incorrectly and in Java, we almost never capitalize a variable name, only Class, Enum, and Interface names.