ArrayList集合到文件改进版本
package com.czie.iot1913.lps.IO.BufferStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/**
* FileName: ArrayListToTxt01
* Author: lps
* Date: 2022/3/26 18:02
* Sign:刘品水 Q:1944900433
*/
public class ArrayListToTxt01 {
public static void main(String[] args) throws IOException {
//创建集合
ArrayList<Students> array = new ArrayList<Students>();
//创建学生对象
Students s1 = new Students("it01","刘品水",20,"镇江");
Students s2 = new Students("it02","刘品火",21,"南京");
//添加进集合
array.add(s1);
array.add(s2);
//创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\JavaCode\\array.txt"));
//遍历集合 得到学生对象
for (Students s:array){
//吧学生对象数据拼接成指定的字符串
StringBuilder sb = new StringBuilder();
sb.append(s.getNumber()+","+s.getName()+","+s.getAge()+","+s.getLiveofplace());
//填写
bw.write(sb.toString());
//bw.write(String.valueOf(sb));
bw.newLine();
bw.flush();
}
bw.close();
}
}