package package3;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class ArrayListToTxtDemo {
public static void main(String[] args) throws IOException {
//创建ArrayList集合
ArrayList<String> list = new ArrayList<>();
//往集合中添加元素
list.add("hello");
list.add("zhangsan");
list.add("wamhgwu");
//创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("day04\\src\\package3\\array.txt"));
//遍历集合,得到每一个字符串数据
for(String s:list){
//调用字符缓冲输出流对象的方法写数据
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
}
}
标签:java,写入,ArrayList,list,bw,文本文件,字符串,import
From: https://www.cnblogs.com/knlf/p/16813891.html