package com.qunar.bean;
import java.io.Serializable;
/**
* 学生实体类
* @author sjf0115
*
*/
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
// 姓名
private String name;
// 学号
private String ID;
// 年龄
private int age;
// 学校
private String school;
/**
* @param name
* @param iD
* @param age
* @param school
*/
public Student(String name, String id, int age, String school) {
super();
this.name = name;
ID = id;
this.age = age;
this.school = school;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
@Override
public String toString() {
return "姓名:" + name + " 学号:" + ID + " 年龄:" + age + " 学校:" + school;
}
}
package com.qunar.io;
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 com.qunar.bean.Student;
public class SeriaCode {
public static void main(String[] args) {
// 对象序列化数据保存位置
String path = "D:\\seria.dat";
try {
// 创建FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream(path);
// 创建ObjectOutputStream对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
// 序列化对象
Student student = new Student("xiaosi","130346",25,"西安电子科技大学");
// 进行对象序列化 Student对象要实现序列化接口
objectOutputStream.writeObject(student);
objectOutputStream.flush();
objectOutputStream.close();
// 创建FileInputStream对象
FileInputStream fileInputStream = new FileInputStream(path);
// 创建ObjectInputStream对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// 反序列化为对象
Student stu = (Student)objectInputStream.readObject();
objectInputStream.close();
System.out.println("Stu->"+stu);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package com.qunar.io.serial;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class SerialCode2 {
public static void main(String[] args) {
try {
// 创建FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("seria.dat");
// 创建ObjectInputStream对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// 反序列化为对象
Object object = objectInputStream.readObject();
objectInputStream.close();
System.out.println(object.getClass());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
java.lang.ClassNotFoundException:com.qunar.io.Student
atjava.net.URLClassLoader$1.run(UnknownSource)
atjava.net.URLClassLoader$1.run(UnknownSource)
atjava.security.AccessController.doPrivileged(NativeMethod)
atjava.net.URLClassLoader.findClass(UnknownSource)
atjava.lang.ClassLoader.loadClass(UnknownSource)
atsun.misc.Launcher$AppClassLoader.loadClass(UnknownSource)
atjava.lang.ClassLoader.loadClass(UnknownSource)
atjava.lang.Class.forName0(NativeMethod)
atjava.lang.Class.forName(UnknownSource)
atjava.io.ObjectInputStream.resolveClass(UnknownSource)
atjava.io.ObjectInputStream.readNonProxyDesc(UnknownSource)
atjava.io.ObjectInputStream.readClassDesc(UnknownSource)
atjava.io.ObjectInputStream.readOrdinaryObject(UnknownSource)
atjava.io.ObjectInputStream.readObject0(UnknownSource)
atjava.io.ObjectInputStream.readObject(UnknownSource)
atcom.qunar.io.serial.SerialCode2.main(SerialCode2.java:17)
|
package com.qunar.io;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Fruit implements Externalizable{
public Fruit(){
System.out.println("Fruit constructor...");
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("Fruit writeExternal...");
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("Fruit readExternal...");
}
}
package com.qunar.io;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Fruit2 implements Externalizable{
Fruit2(){
System.out.println("Fruit2 constuctor...");
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("Fruit writeExternal...");
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("Fruit readExternal...");
}
}
package com.qunar.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class FruitSerialCode {
public static void main(String[] args) {
try {
// 创建FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream("fruit.out");
// 创建ObjectOutputStream对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
// 序列化对象
Fruit fruit = new Fruit();
Fruit2 fruit2 = new Fruit2();
// 进行对象序列化 Fruit对象要实现序列化接口
System.out.println("writeObject...");
objectOutputStream.writeObject(fruit);
objectOutputStream.writeObject(fruit2);
objectOutputStream.flush();
objectOutputStream.close();
// 创建FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("fruit.out");
// 创建ObjectInputStream对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// 反序列化为对象
System.out.println("readFruit...");
fruit = (Fruit)objectInputStream.readObject();
System.out.println("readFruit2...");
fruit2 = (Fruit2)objectInputStream.readObject();
objectInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Fruitconstructor...
Fruit2constuctor...
writeObject...
FruitwriteExternal...
FruitwriteExternal...
readFruit...
Fruitconstructor...
FruitreadExternal...
readFruit2...
java.io.InvalidClassException:com.qunar.io.Fruit2;novalidconstructor
atjava.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(UnknownSource)
atjava.io.ObjectStreamClass.checkDeserialize(UnknownSource)
atjava.io.ObjectInputStream.readOrdinaryObject(UnknownSource)
atjava.io.ObjectInputStream.readObject0(UnknownSource)
atjava.io.ObjectInputStream.readObject(UnknownSource)
atcom.qunar.io.FruitSerialCode.main(FruitSerialCode.java:36)
|
所有默认的构造函数都会被调用,才能使Externalizable对象产生正确的行为。 |
package com.qunar.io;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Fruit implements Externalizable{
private String name;
private int num;
// 必须有默认构造函数 反序列时使用
public Fruit(){
System.out.println("Fruit default constructor...");
}
/**
* @param name
* @param num
*/
public Fruit(String name, int num) {
System.out.println("Fruit constructor...");
this.name = name;
this.num = num;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("Fruit writeExternal...");
// 必须做如下操作
out.writeObject(name);
out.writeInt(num);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("Fruit readExternal...");
// 必须做如下操作
name = (String)in.readObject();
num = in.readInt();
}
@Override
public String toString() {
return "name:" + name + " num:" + num;
}
}
package com.qunar.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class FruitSerialCode {
public static void main(String[] args) {
try {
// 创建FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream("fruit.out");
// 创建ObjectOutputStream对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
// 序列化对象
Fruit fruit = new Fruit("苹果",20);
// 进行对象序列化 Fruit对象要实现序列化接口
System.out.println("writeObject...");
objectOutputStream.writeObject(fruit);
objectOutputStream.flush();
objectOutputStream.close();
// 创建FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("fruit.out");
// 创建ObjectInputStream对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// 反序列化为对象
System.out.println("readFruit...");
fruit = (Fruit)objectInputStream.readObject();
System.out.println("Fruit->[" + fruit + "]");
objectInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Fruitconstructor...
writeObject...
FruitwriteExternal...
readFruit...
Fruitdefaultconstructor...
FruitreadExternal...
Fruit->[name:苹果num:20]
|
Fruitconstructor...
writeObject...
FruitwriteExternal...
readFruit...
Fruitdefaultconstructor...
FruitreadExternal...
Fruit->[name:nullnum:0]
|
package com.qunar.io;
import java.io.Serializable;
import java.util.Date;
public class Login implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Date date = new Date();
private String userName;
// 防止被序列化transient
private transient String password;
/**
* @param date
* @param userName
* @param password
*/
public Login(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
@Override
public String toString() {
return "Date:" + date + " UserName:" + userName + " Password:" + password;
}
}
package com.qunar.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class LoginSerialCode {
public static void main(String[] args) {
try {
// 创建FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream("login.out");
// 创建ObjectOutputStream对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
// 序列化对象
Login login = new Login("xiaosi", "123");
// 进行对象序列化 Fruit对象要实现序列化接口
System.out.println("writeObject...");
objectOutputStream.writeObject(login);
objectOutputStream.flush();
objectOutputStream.close();
// 创建FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("login.out");
// 创建ObjectInputStream对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// 反序列化为对象
System.out.println("readFruit...");
login = (Login)objectInputStream.readObject();
System.out.println("LoginInfo->[" + login + "]");
objectInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
writeObject...
readFruit...
LoginInfo->[Date:ThuDec3100:16:13CST2015UserName:xiaosiPassword:
null
]
|
package com.qunar.io;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class Login implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Date date = new Date();
private String userName;
// 防止被序列化transient
private transient String password;
/**
* @param date
* @param userName
* @param password
*/
public Login(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
// 必须有
private void writeObject(ObjectOutputStream stream) throws IOException{
// 默认的序列化
stream.defaultWriteObject();
// 手动完成序列化
stream.writeObject(password);
}
// 必须有
private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException{
// 默认的反序列化
stream.defaultReadObject();
// 手动完成反序列化
password = (String)stream.readObject();
}
@Override
public String toString() {
return "Date:" + date + " UserName:" + userName + " Password:" + password;
}
}
package com.qunar.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class LoginSerialCode {
public static void main(String[] args) {
try {
// 创建FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream("login.out");
// 创建ObjectOutputStream对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
// 序列化对象
Login login = new Login("xiaosi", "123");
// 进行对象序列化 Fruit对象要实现序列化接口
System.out.println("writeObject...");
objectOutputStream.writeObject(login);
objectOutputStream.flush();
objectOutputStream.close();
// 创建FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("login.out");
// 创建ObjectInputStream对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// 反序列化为对象
System.out.println("readFruit...");
login = (Login)objectInputStream.readObject();
System.out.println("LoginInfo->[" + login + "]");
objectInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
writeObject...
readFruit...
LoginInfo->[Date:FriJan0115:57:53CST2016UserName:xiaosi
Password:123
]
|
package com.qunar.io;
import java.io.Serializable;
public class House implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String location;
/**
* @param name
* @param location
*/
public House(String name, String location) {
super();
this.name = name;
this.location = location;
}
}
package com.qunar.io;
import java.io.Serializable;
public class Animal implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private House house;
/**
* 构造函数
* @param name
* @param house
*/
public Animal(String name, House house) {
super();
this.name = name;
this.house = house;
}
@Override
public String toString() {
return name + " " + house;
}
}
package com.qunar.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class AnimalSerialCode {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
House house = new House("水立方","北京海淀区");
List<Animal> animals = new ArrayList<Animal>();
animals.add(new Animal("狗", house));
animals.add(new Animal("鸡", house));
animals.add(new Animal("羊", house));
System.out.println("Animals->" + animals);
try {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(buf);
// 序列化
objectOutputStream.writeObject(animals);
objectOutputStream.writeObject(animals);
ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(buf2);
// 序列化
objectOutputStream2.writeObject(animals);
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
List<Animal> ani1 = (List<Animal>)objectInputStream.readObject();
List<Animal> ani2 = (List<Animal>)objectInputStream.readObject();
ObjectInputStream objectInputStream2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
List<Animal> ani3 = (List<Animal>)objectInputStream2.readObject();
System.out.println("Animals1->"+ani1);
System.out.println("Animals2->"+ani2);
System.out.println("Animals3->"+ani3);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Animals->[狗com.qunar.io.House@1b15692,鸡com.qunar.io.House@1b15692,羊com.qunar.io.House@1b15692]
Animals1->[狗com.qunar.io.House@1aaf0b3,鸡com.qunar.io.House@1aaf0b3,羊com.qunar.io.House@1aaf0b3]
Animals2->[狗com.qunar.io.House@1aaf0b3,鸡com.qunar.io.House@1aaf0b3,羊com.qunar.io.House@1aaf0b3]
Animals3->[狗com.qunar.io.House@1a082e2,鸡com.qunar.io.House@1a082e2,羊com.qunar.io.House@1a082e2]
|