#Tablesaw #Java 在导出为CSV时,表格被缩减为列表的最后一个元素。

huangapple go评论81阅读模式
英文:

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

问题

我自学Java,正在尝试编写一些工作程序。
就像标题中所说,表格看起来没问题。
但是当我导出为Csv时,只有最后一个元素被保存。
我一定是在某个地方犯了错误。
您的帮助将不胜感激。

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");

    Adresses.clear();
    Names.clear();
    t.clear();
    column0.clear();
    column1.clear();
}
英文:

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&lt;String&gt; Adresses = new ArrayList&lt;&gt;();
    List&lt;String&gt; Names = new ArrayList&lt;&gt;();
    
    for (int i = 0; i &lt; listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            Adresses.add(listOfFiles[i].getAbsolutePath());
            Names.add(listOfFiles[i].getName());
            }
        
        StringColumn column0 = StringColumn.create(&quot;Names&quot;, Names);
        StringColumn column1 = StringColumn.create(&quot;Addresses&quot;,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(&quot;C:\\Users\\user\\Desktop\\Dossier\\Floweriest.csv&quot;);

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

答案1

得分: 0

你的CSV文件在每次迭代中都会被覆盖,正如@fantaghirocco所建议的。

我并不完全清楚你试图做什么。如果你想要每次迭代都有一个单独的输出文件,你需要在循环中为每次迭代提供一个新的文件名。

如果你想要从所有其他文件中提取数据生成一个输出CSV文件,你需要在循环之外设置表格等内容。像这样:

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");
}

如果这是你想要做的事情,你也可以跳过列表,创建循环之前的表格,然后使用.append()方法将结果直接添加到列中。

最后,有几个小点需要注意。Adresses的拼写有错误,在Java中,我们几乎从不对变量名进行大写,只有类(Class)、枚举(Enum)和接口(Interface)名会大写。

英文:

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&lt;String&gt; Adresses = new ArrayList&lt;&gt;();
    List&lt;String&gt; Names = new ArrayList&lt;&gt;();

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

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

    Table t = Table.create();

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

    t.write().csv(&quot;C:\\Users\\user\\Desktop\\Dossier\\Floweriest.csv&quot;);
}

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.

huangapple
  • 本文由 发表于 2020年4月7日 22:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61082982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定