Java序列化的实现很简单,须要序列化的类实现Serializable接口;java
而后就可使用ObjectOutStream的write()方法输出;app
反序列化:使用 ObjectInputStream的read()方法便可输入;iphone
序列化和反序列化实现很简单,可是要注意实现Serializable接口的类中不能有其余类的实例化对象;ide
即:若是这个类中还包含别的类的实例,那么被调用的类也须要实现Serializable接口。this
下面就是一个我在学序列化时,遇到的麻烦:spa
嫌麻烦,在Serializable接口的类中实例化了Scanner类的对象,致使运行错误;code
做业需求:对象
应用对象序列化和对象反序列化向文件写入对象,并将对象读取出来输入到控制台上。blog
效果图: 继承
任务要求:
一、建立Product类,并声明它的属性:ID,name,categories,price
二、继承Serializable接口;
三、定义Product类构造方法;
四、在Test类里面,建立Product类的对象:iphone,ipad,macbook,iwatch
五、实例化对象输入流和对象输出流;
六、写入Product类的四个对象;
七、从文件里读取四个product类的四个对象。
个人代码:
1 import java.io.Serializable; 2 import java.util.Scanner; 3 4 public class Apple implements Serializable { 5 private String id; 6 private String name; 7 private String categories; 8 private double price; 9 Scanner sc = new Scanner(System.in); 10 public Apple(String id, String name, String categories, double price) { 11 this.setId(id); 12 this.setName(name); 13 this.setCategories(categories); 14 this.setPrice(price); 15 } 16 public String getId() { 17 return id; 18 } 19 public void setId(String id) { 20 this.id = id; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public String getCategories() { 29 return categories; 30 } 31 public void setCategories(String categories) { 32 this.categories = categories; 33 } 34 public double getPrice() { 35 return price; 36 } 37 public void setPrice(double price) { 38 this.price = price; 39 } 40 @Override 41 public String toString() { 42 return "产品ID:" + id + "\n产品名称:" + name + "\n产品属性:" + categories + "\n产品价格:" + price+"\n"; 43 } 44 45 }
package com.task8; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; public class FileTest4 { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("apple.txt"); FileInputStream fis = new FileInputStream("apple.txt"); ObjectOutputStream obs = new ObjectOutputStream(fos); ObjectInputStream ois = new ObjectInputStream(fis); obs.writeObject(new Apple("123","iphone","telephone",4888.0)); obs.writeObject(new Apple("234","ipad","computer",5088.0)); obs.writeObject(new Apple("345","macbook","computer",10688.0)); obs.writeObject(new Apple("256","iwatch","watch",4799.0)); System.out.println("apple系列产品信息:"); for(int i = 0;i<4;i++) { try { System.out.println(ois.readObject()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } fos.close(); fis.close(); obs.close(); ois.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果:
重要的事说三遍:
实现Serializable接口的类中不能有其余类的实例化对象;
实现Serializable接口的类中不能有其余类的实例化对象;
实现Serializable接口的类中不能有其余类的实例化对象;
即:若是这个类中还包含别的类的实例,那么被调用的类也须要实现Serializable接口。