Java 如何将链表中的下一个元素打印到CSV文件?

Java 如何将链表中的下一个元素打印到CSV文件?,java,string,linked-list,printwriter,Java,String,Linked List,Printwriter,我正在制作一个地址簿,我的程序应该将列表中的每个元素保存到一个CSV文件中。我已经得到了一切工作助理,因为它将只保存一行到文件中 public static void save(){ PrintWriter writer = null; try { writer = new PrintWriter("C:\\Users\\Remixt\\workspace\\2\\AddressBook.csv", "UTF-8"); } catch (FileNotFoundException e)

我正在制作一个地址簿,我的程序应该将列表中的每个元素保存到一个CSV文件中。我已经得到了一切工作助理,因为它将只保存一行到文件中

public static void save(){
PrintWriter writer = null;
try {
    writer = new PrintWriter("C:\\Users\\Remixt\\workspace\\2\\AddressBook.csv", "UTF-8");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.exit(0);
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.exit(0);
}
 {
    writer.println(AddressBook.get(getListSize()-1)+"\n");


writer.close();//saves file
}
编辑:它只将最后一个元素保存到文件中。无论我在列表中添加了多少次其他内容,它都只显示文件中的一项内容。

问题就在这里

writer.println(AddressBook.get(getListSize()-1)+"\n");
您只需将
AddressBook
的最后一个元素写入
csv
文件,使用
for loop

下面是一个示例

for (int i = 0; i < AddressBook.size(); i++) {
     writer.println(AddressBook.get(i)+"\n");

}
问题就在这里

writer.println(AddressBook.get(getListSize()-1)+"\n");
您只需将
AddressBook
的最后一个元素写入
csv
文件,使用
for loop

下面是一个示例

for (int i = 0; i < AddressBook.size(); i++) {
     writer.println(AddressBook.get(i)+"\n");

}