【发布时间】:2015-03-02 19:16:13
【问题描述】:
我在使用 printwriter 将数组列表写入文件时遇到了一些问题。我尝试了另一种可行的方法,但它不会只打印数组列表中的所有内容。这是我目前正在尝试的方式,它不会打印任何内容。
datArrayList = new ArrayList<theAccounts>();
File file = new File("output.txt");
public void writer() throws FileNotFoundException, IOException{
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
FileOutputStream fo = new FileOutputStream(file);
int datList = datArrayList.size();
for (int i = 0; i < datList; i++){
pw.write(datArrayList.get(i).toString() + "\n");
}
谁能告诉我应该怎么做才能将数组中的所有项目写入输出文件?谢谢你:)
【问题讨论】:
-
要将对象写入文件,您应该考虑序列化而不是文本。
-
您正在为同一个文件打开两个 FileOutputStreams,并且只使用其中一个。删除你不使用的那个。
-
您发布的代码不完整。发布MCVE
-
close()您的流,或使用try-with-resources 语句。 -
datArrayList中有没有对象???
标签: java arrays arraylist printwriter