Java 将所有名称写入 csv 文件

Java write all name into csv file

变化如何:

namelist = name 

namelist = namelist + " " + name

并且只调用一次 writeFile 方法。

或者您可以将 namelist 声明为 StringBuilder,并使用 append() 方法做同样的事情。

创建名称的链接列表:将 String nameList; 替换为 LinkedList<String> names = new LinkedList<>();

将每个名称添加到列表中:将 nameList = name; 替换为 names.add(records[0]);

然后将名称添加到新文件中:

public void writeFile(String fileName, List<String> names) throws IOException{

File file = new File(fileName);
FileWriter fileWriter = new FileWriter(file);
for(String name: names){
    filewriter.write(name);//writes the current name to the file. you may need to add a /n or a "," to the name to get approprite line seperations and comas
}

fileWriter.flush();
fileWriter.close();
}