Java 对象流(输入-输出)objectoutputstream序列化报错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 对象流(输入-输出)objectoutputstream序列化报错相关的知识,希望对你有一定的参考价值。

报错:java.io.notserializableexception

解决方法:添加下面代码实现

技术分享

对象类

 1 package com.etc._07ObjectDemo;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Person implements Serializable {
 6     private int id;
 7     private String name;
 8     private String sex;
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public String getSex() {
22         return sex;
23     }
24     public void setSex(String sex) {
25         this.sex = sex;
26     }
27     public Person() {
28         super();
29         // TODO Auto-generated constructor stub
30     }
31     public Person(int id, String name, String sex) {
32         super();
33         this.id = id;
34         this.name = name;
35         this.sex = sex;
36     }
37     @Override
38     public String toString() {
39         return "Person [id=" + id + ", name=" + name + ", sex=" + sex + "]";
40     }
41     
42     
43 
44 }
Object输入流
 1 package com.etc._07ObjectDemo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.io.ObjectInputStream;
 7 
 8 public class ObjectInputStreamDemo {
 9     //文件-------->Person对象     反序列化
10     public static void main(String[] args) {
11         try {
12             ObjectInputStream ois = new ObjectInputStream(
13                     new FileInputStream("D:/my123.ini"));
14             
15             Object object = ois.readObject();
16             if(object instanceof Person){
17                 Person p = (Person)object;
18                 System.out.println(p);
19             }
20             
21             
22             ois.close();
23         } catch (FileNotFoundException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         } catch (IOException e) {
27             // TODO Auto-generated catch block
28             e.printStackTrace();
29         } catch (ClassNotFoundException e) {
30             // TODO Auto-generated catch block
31             e.printStackTrace();
32         }
33     }
34 
35 }
Object输出流
 1 package com.etc._07ObjectDemo;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.io.ObjectOutputStream;
 8 
 9 public class ObjectOutputStreamDemo {
10     
11     //Person对象------>文件     序列化
12     public static void main(String[] args) {
13         
14         try {
15             ObjectOutputStream oos = new ObjectOutputStream(
16                     new FileOutputStream("D:/my123.ini"));
17 //            创建对象
18             Person p = new Person(1001,"张三","");
19             oos.writeObject(p);
20             oos.close();
21             
22             System.out.println("ending....");
23             
24             //java.io.NotSerializableException
25         } catch (FileNotFoundException e) {
26             // TODO Auto-generated catch block
27             e.printStackTrace();
28         } catch (IOException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32         
33     }
34 
35 }

 

以上是关于Java 对象流(输入-输出)objectoutputstream序列化报错的主要内容,如果未能解决你的问题,请参考以下文章

Java中IO对象的输入输出流

Java实现文件写入——IO流(输入输出流详解)

从零开始的Java开发1-6-4 Java输入输出流:File类绝对路径和相对路径字节流缓冲流字符流对象序列化

java Zip压缩输入输出流问题

Java输入输出流详解

java--标准输入输出流