欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

从集合到文件

时间:2023-08-05

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(); }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。