会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132390个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 663楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 664楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 665楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 666楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 668楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 669楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 670楼

package object;

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

public class TestClasses {
    public static void main(String[] args) throws IOException {
//        write();
        read();
    }

    public static void write() throws IOException {
        //创建班级对象
        ArrayList<Student> al = new ArrayList<Student>();
        al.add(new Student("marry", 20, "88888888888"));
        al.add(new Student("lili", 21, "123456"));
        al.add(new Student("jack", 22, "654321"));
        Classes cl = new Classes("JN101", al);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e:/classes.txt"));
        oos.writeObject(cl);
        oos.close();

    }

    public static void read() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("e:/classes.txt"));
            Classes cl2 = (Classes) ois.readObject();
            System.out.println(cl2.getClassName()+"\t"+cl2.getAl());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这里如果读入的时候不这么写

 System.out.println(cl2.getClassName()+"\t"+cl2.getAl());

而是在classes类中写toString方法就会产生异常为什么

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 671楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 673楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637