将集合中的学生对象输出到指定文件中




package day10_18;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class CopyStudentDate {
public static void main(String[] args) throws IOException {
ArrayList<Student>arrayList=new ArrayList<>();
Student student1=new Student("001","张三丰",18,"江西南昌");
Student student2=new Student("002","李连杰",19,"江西南丰");
Student student3=new Student("003","周星驰",28,"江西赣州");
arrayList.add(student1);
arrayList.add(student2);
arrayList.add(student3);
BufferedWriter bf=new BufferedWriter(new FileWriter("Gzy_BasicJava\student.txt"));
for (Student s:arrayList){
String ss=s.getId()+s.getName()+s.getAge()+s.getAddress();
bf.write(ss+" ");
}
bf.close();



package day10_18;
public class Student {
private String id;
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}

private int age;
private String address;
}

运行效果

 

 



}
}

原文地址:https://www.cnblogs.com/gzy918/p/13837863.html