0
点赞
收藏
分享

微信扫一扫

从集合到文件

小布_cvg 2022-02-05 阅读 46
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class ArrayListToTxtDemo2 {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> array = new ArrayList<Student>();
        Student s1 = new Student("19031235", "郭靖", 22, "西安");
        Student s2 = new Student("19031236", "黄蓉", 22, "西安");
        Student s3 = new Student("19031237", "张三丰", 22, "西安");
        Student s4 = new Student("19031238", "谢逊", 22, "西安");
        array.add(s1);
        array.add(s2);
        array.add(s3);
        array.add(s4);
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Java\\java作业\\myCharStream\\abc.txt"));


        for (Student s : array) {
            StringBuilder sb = new StringBuilder();
            sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
举报

相关推荐

0 条评论