码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-05 01:46:16      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:cti   instance   height   fileinput   创建对象   ble   代码实现   turn   stat   

报错: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序列化报错

标签:cti   instance   height   fileinput   创建对象   ble   代码实现   turn   stat   

原文地址:http://www.cnblogs.com/bosskaiche/p/7476456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!