当前位置: 首页> 文旅> 艺术 > Java超详细知识点——序列化和反序列化

Java超详细知识点——序列化和反序列化

时间:2025/7/11 14:42:43来源:https://blog.csdn.net/qq_62859013/article/details/142103104 浏览次数:0次

还有一点二进制读写我放最前面了。

二进制读写DataInputStream:

类之间的关系:

读取:InputStream——>FileInputStream——>DataInputStream(InputStream)

写入:OutputStream——FileOutputStream——>DataputStream(OutputStream)

二进制复制图片

如果图片很大就很会慢

public static void main(String[] args) {DataInputStream dis = null;DataOutputStream dos = null;try {dis = new DataInputStream(new FileInputStream("D:\\sky.JPG"));dos = new DataOutputStream(new FileOutputStream("D:\\ch06/skyCopy.JPG"));int b = -1;while((b=(dis.read()))!=-1){dos.write(b);}System.out.println("复制成功");} catch (Exception e) {e.printStackTrace();}finally {try {dos.close();dis.close();} catch (IOException e) {e.printStackTrace();}}}

序列化和反序列化:

序列化:

将内存对象存储到硬盘 永久的保存到磁盘上,这就是序列化。

将硬盘中存储的对象转换为内存对象,这就是反序列化。

对象序列化机制可以使内存中的Java对象转换成与平台无关的二进制流,既可以将这种二进制流持久地保存在磁盘上,也可以通过反序列化恢复成原来的Java对象。

如果想让某个对象支持序列化机制,那么这个对象所在的类必须是可序列化的。在Java中,序列化的类必须实现Serializable或Externalizable两个接口之一。

将内存对象存储到硬盘:序列化

OutputStream——>ObjectOutputStream(OutputStream)

OutputStream——>FileOutputStream

将硬盘中存储的对象转换为内存对象:反序列化

InputStream——>ObjectInputStream(InputStream)

InputStream——>FileInputStream

序列化和反序列化:

  1. oos.writeObject(student);student 对象序列化并写入到 ObjectOutputStream
  2. ois.readObject(); 从输入流中读取一个对象,并将其转换为 Java 对象。 需要进行类型转换
public static void main(String[] args) {ObjectInputStream  ois= null;ObjectOutputStream  oos = null;try {//序列化oos = new ObjectOutputStream(new FileOutputStream("D:\\ch06/1.txt"));Student student = new Student("蛋蛋怪",20);//写入对象oos.writeObject(student);System.out.println("写入成功!!!");//Student类必须得实现Student implements Serializable//反序列化ois = new ObjectInputStream(new FileInputStream("D:\\ch06/1.txt"));Student stu = (Student) ois.readObject();System.out.println(stu.toString());} catch (Exception e) {e.printStackTrace();}finally {try {oos.close();ois.close();} catch (Exception e) {e.printStackTrace();}}}

序列化:

步骤:

1.这个student类如果想要序列化,那么就必须要实现Serializable接口或者Externalizable接口

2.创建一个输出流 ObjectOutputStream对象

3.通过输出流的.writeObject()方法写入文件

4.关闭输出流

//对象序列化,写入输出流
objectOutputStream.writeObject(list);

一个对象序列化:

(这里的try-catch可以用throws代替)

  public static void main(String[] args) {ObjectOutputStream objectOutputStream = null;//创建一个对象输出流try {objectOutputStream = new ObjectOutputStream(new FileOutputStream("writer.txt"));Student student = new Student("陈梦雨",20,"女");//将对象序列化objectOutputStream.writeObject(student);} catch (IOException e) {e.printStackTrace();}finally {if (objectOutputStream!= null){try {objectOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}

多个对象序列化:

使用集合保存对象,可以将集合中的所有对象序列化

  public static void main(String[] args) throws IOException {//保存多个学生对象ObjectOutputStream objectOutputStream = null;//创建一个对象输出流objectOutputStream = new ObjectOutputStream(new FileOutputStream("writer.txt"));Student student = new Student("陈梦雨", 20, "女");Student student2 = new Student("豆豆", 21, "女");ArrayList<Student> list = new ArrayList<Student>();list.add(student);list.add(student2);//对象序列化,写入输出流objectOutputStream.writeObject(list);}

反序列化:

如果向文件中使用序列化机制写入多个对象,那么反序列化恢复对象时,必须按照写入的顺序读取

步骤:

1.实现Serializable接口

2.创建对象输入流

3.调用readObject()方法,读取对象

4.关闭输入流

一个对象反序列化:
 public static void main(String[] args) throws Exception {//反序列化,输出ObjectInputStream objectInputStream =null;//创建objectInputStream输入流objectInputStream = new ObjectInputStream(new FileInputStream("writer.txt"));//反序列化,进行强转Student stu = (Student) objectInputStream.readObject();//输出生成后的信息System.out.println("姓名"+stu.getName());System.out.println("年龄"+stu.getAge());System.out.println("性别为"+stu.getSex());if (objectInputStream!=null){objectInputStream.close();}}

多个对象反序列化:
public static void main(String[] args) throws IOException, ClassNotFoundException {//创建对象ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("writer.txt"));//反序列化进行强制类型转换ArrayList<Student> students = (ArrayList<Student>) objectInputStream.readObject();// Student stu = (Student) objectInputStream.readObject();//输出对象信息for (Student stu :students){System.out.println("姓名"+stu.getName());System.out.println("年龄"+stu.getAge());System.out.println("性别为"+stu.getSex());}}

关键字:Java超详细知识点——序列化和反序列化

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: