从集合到文件

从集合到文件,第1张

集合到文件
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 array = new ArrayList();
        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();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5722484.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存