了解序列化流,实现效果:将若干个Student对象添加到List集合中,然后将List存储到txt文件中,再从txt文件读取内容,并且打印List。(这是手动实现序列化和反序列化过程)

import java.io.*;
import java.util.ArrayList;
import java.util.List;

class Student implements Serializable {
    private String id;
    private String name;
    private int age;
    private String className;

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

    // Getters and setters

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", className='" + className + '\'' +
                '}';
    }
}

public class Main {
    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("1", "Alice", 18, "Class A"));
        studentList.add(new Student("2", "Bob", 20, "Class B"));
        studentList.add(new Student("3", "Charlie", 22, "Class C"));

        String fileName = "students.txt";

        // 将 List 存储到文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            for (Student student : studentList) {
                writer.write(student.getId() + "," + student.getName() + "," + student.getAge() + "," + student.getClassName());
                writer.newLine();
            }
            System.out.println("List 存储到文件成功");
        } catch (IOException e) {
            System.out.println("List 存储到文件失败: " + e.getMessage());
        }

        // 从文件读取内容并打印 List
        List<Student> readList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
 //检查 parts 数组的长度是否为 4,以确保读取到的行包含了正确的学生属性信息。如果长度为 4,我们可以假设这是一个正确格式的行,其中包含了学生的 id、姓名、年龄和班级。
                if (parts.length == 4) {   
                    String id = parts[0];  
                    String name = parts[1];
                    int age = Integer.parseInt(parts[2]);
                    String className = parts[3];
                    Student student = new Student(id, name, age, className);
                    readList.add(student);
                }
            }
            System.out.println("\n从文件读取的 List:");
            for (Student student : readList) {
                System.out.println(student);
            }
        } catch (IOException e) {
            System.out.println("从文件读取内容失败: " + e.getMessage());
        }
    }
}

使用 BufferedWriterFileWriter 将学生列表逐行写入文本文件中。每行包含学生的各个属性,用逗号分隔。

然后,我们使用 BufferedReaderFileReader 从文件中逐行读取内容,并根据逗号分隔的属性创建学生对象,将它们添加到读取列表中。

最后,我们遍历读取的列表并打印每个学生对象。

请确保运行代码时,文件名指定的路径下有读写权限。运行示例代码后,将会将 studentList 写入到文件中,并从文件中读取内容并打印列表中的学生对象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值