[英]How do I print the next element in a linked list to a CSV file?
I'm making an address book and my program is supposed to save each element in a list to a CSV file. 我正在制作通讯录,我的程序应该将列表中的每个元素保存到CSV文件。 I've gotten everything to work asside from the fact that it will only save 1 line to the file. 除了将只保存1行到文件这一事实之外,我还拥有所有其他功能。
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
}
Edit: It will only save the last element to the file. 编辑:它将仅将最后一个元素保存到文件。 It only shows 1 thing in the file no matter how many times i add something else to the list. 无论我向列表中添加其他内容多少次,它仅在文件中显示1件事。
the problem is here 问题在这里
writer.println(AddressBook.get(getListSize()-1)+"\n");
you just write the last element of AddressBook
to the csv
file, use for loop
您只需将AddressBook
的最后一个元素写入csv
文件,即可使用for loop
the following is a sample 以下是一个示例
for (int i = 0; i < AddressBook.size(); i++) {
writer.println(AddressBook.get(i)+"\n");
}
at last, you should write file by append mode
最后,您应该通过append mode
写入文件
filename=new FileWriter("printWriter.txt",true);
writer=new java.io.PrintWriter(filename);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.