集合与文件IO流结合的相关实例

用代码实现以下需求

(1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法
(2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
(3)将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
(4)读取D:\\StudentInfo.txt文件中的ArrayList对象
(5)对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
(6)将ArrayList集合中排序后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式:张三-男-25)

学生类:

public class Student {
    private String name;
    private String gender;
    private  int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "学生[" + "姓名=" + name + ", 性别=" + gender + ", 年龄=" + age + ']';
    }
}

测试类:

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("idea_test[关卡8--16]\\src\\work14\\SpecialstreamAndPropertiespropertyset\\lianxi5\\StudentInfo.txt");
        //键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
        Scanner scanner = new Scanner(System.in);
        //创建ArrayList集合
        ArrayList<Student> arrayList = new ArrayList<>();
        System.out.println("请输入6个学员信息(逗号隔开),需要两个相同信息。(录入格式:张三,男,25)");
        for (int i=1;i<=6;i++){
            System.out.println("请输入第"+i+"个学员信息:");
            String line = scanner.nextLine();
            //定义数组
            String[] split = line.split(",");
            //设置姓名,性别,年龄的下标:
            String name = split[0];
            String gender = split[1];
            String nl= split[2];
            int age = Integer.parseInt(nl);//数据类型转换
            //将数据放入集合对象中
            arrayList.add(new Student(name,gender,age));
        }

        //将存有6个学员信息的ArrayList集合对象写入到StudentInfo.txt文件中
        //创建字符输出对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (Student student : arrayList){
            //拼接学生信息
            String line = student.getName()+","+student.getGender()+","+student.getAge();
            //写入
            bw.write(line);
            bw.write("\r\n");//换行
            bw.flush();
        }
        //释放资源
        bw.close();

        //读取StudentInfo.txt文件中的ArrayList对象
        //创建字符输入流对象
        BufferedReader fr = new BufferedReader(new FileReader(file));
        System.out.println("StudentInfo.txt文件中的ArrayList对象为:");
        String line;
        while ((line=fr.readLine())!=null){
            System.out.println(line);
        }

        //对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getAge()-o2.getAge();
                return num;
            }
        });
        for (Student student : arrayList){
            treeSet.add(student);
        }

        //将ArrayList集合中排序后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式:张三-男-25)
        PrintWriter pw = new PrintWriter(new FileWriter("idea_test[关卡8--16]\\src\\work14\\SpecialstreamAndPropertiespropertyset\\lianxi5\\StudentInfo1.txt"));
        for (Student student : treeSet){
            //拼接学员信息
            String line1 = student.getName()+"-"+student.getGender()+"-"+student.getAge();
            //写入
            pw.println(line1);
        }
        //释放资源
        pw.close();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值