练习_序列化结合和打印流_概述和使用
练习:序列化集合
当我们想在文件中保存多个对象的时候
可以把多个对象存储到一个集合中
对集合进行序列化和反序列化
分析:
1.定义一个存储Person对象的ArrayList集合
2.往ArrayList集合中存储Person对象
3.创建一个序列化流ObjectOutputStream对象
4.使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化
5.创建一个反序列化流ObjectInputStream对象
6.使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
7.把Object类型的集合转换为ArrayList类型
8.遍历ArrayList集合
9.释放资源
public class Demo03Text { public static void main(String args[]) throws IOException, ClassNotFoundException { //1.定义一个存储Person对象的ArrayList集合 ArrayList<Person> person = new ArrayList<>(); //2.往ArrayList集合中存储Person对象 person.add(new Person("夜轻染",18)); person.add(new Person("容景",17)); person.add(new Person("苍亭",20)); //3.创建一个序列化流ObjectOutputStream对象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day10_IO//list.txt")); // 4.使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化 oos.writeObject(person); //5.创建一个反序列化流ObjectInputStream对象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day10_IO//list.txt")); //6.使用ObjectInputStream对象中的方法readObject读取文件中保存的集合 Object o = ois.readObject(); //7.把Object类型的集合转换为ArrayList类型 ArrayList<Person> list2 = (ArrayList<Person>)o; //8.遍历ArrayList集合 for (Person p : list2) { System.out.println(p); } //9.释放资源 oos.close(); ois.close(); } }
java.io.PrintStream:打印流
PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式
PrintStream特点:
1.只负责数据的输出,不负责数据的读取
2.与其他输出流不同,PrintStream 永远不会抛出 IOException
3.有特有的方法,print,println
void print(任意类型的值)
void println(任意类型的值并换行)
构造方法:
PrintStream(File file):输出的目的地是一个文件
PrintStream(OutputStream out):输出的目的地是一个字节输出流
PrintStream(String fileName) :输出的目的地是一个文件路径
PrintStream extends OutputStream
继承自父类的成员方法:
- public void close() :关闭此输出流并释放与此流相关联的任何系统资源
- public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出
- public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流
- public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流
- public abstract void write(int b) :将指定的字节输出流
注意:
如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表 97->a
如果使用自己特有的方法print/println方法写数据,写的数据原样输出 97->97
public class Demo01PrintStream { public static void main(String[] args) throws FileNotFoundException { //System.out.println("HelloWorld"); //创建打印流PrintStream对象,构造方法中绑定要输出的目的地 PrintStream ps = new PrintStream("print.txt"); //如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表 97->a ps.write(97); //如果使用自己特有的方法print/println方法写数据,写的数据原样输出 97->97 ps.println(97); ps.println(8.8); ps.println('a'); ps.println("HelloWorld"); ps.println(true); //释放资源 ps.close(); } }
可以改变输出语句的目的地(打印流的流向)
输出语句,默认在控制台输出
使用System.setOut方法改变输出语句的目的地改为参数中传递的打印流的目的地
static void setOut(PrintStream out)
重新分配“标准”输出流
public class Demo02PrintStream { public static void main(String[] args) throws FileNotFoundException { System.out.println("我是在控制台输出"); PrintStream ps = new PrintStream("目的地是打印流.txt"); System.setOut(ps);//把输出语句的目的地改变为打印流的目的地 System.out.println("我在打印流的目的地中输出"); ps.close(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)