gpt4 book ai didi

java - 将数组列表写入文件

转载 作者:行者123 更新时间:2023-12-01 16:59:28 25 4
gpt4 key购买 nike

我在使用 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");
}

谁能告诉我应该做什么才能将数组中的所有项目写入输出文件?谢谢你:)

最佳答案

datArrayList = new ArrayList<theAccounts>();
File file = new File("output.txt");

public void writer() throws FileNotFoundException, IOException {
FileOutputStream fo = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fo);
int datList = datArrayList.size();

for (theAccounts elem : datArrayList){
pw.println(elem);
}
pw.close();
fo.close();
}

关于java - 将数组列表写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28817715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com