英文:
#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<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();
}
}
答案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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论