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





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

    运行效果

     

     



    }
    }

  • 相关阅读:
    Linux下常用的3种软件安装方式
    解决navicate 连接mysql数据库中文乱码的问题
    Lua 遍历Linux目录下的文件夹
    ubuntu 更改源
    ubuntu 下安装配置LAMP
    简述configure、pkg-config、pkg_config_path三者的关系
    linux 下库的深入调研
    Linux下的库操作工具-nm、ar、ldd、ldconfig和ld.so
    linux命令strings
    c++隐式类型转换和explicit
  • 原文地址:https://www.cnblogs.com/gzy918/p/13837863.html
Copyright © 2020-2023  润新知