天天看點

對象序列化 代碼

 Student.java

package ZHANG.IO.Serialization;

import java.io.Serializable;

public class Student implements Serializable {

int id;

String name;

int age;

String department;

public Student(int id, String name, int age, String department) {

this.id = id;

this.name = name;

this.age = age;

this.department = department;

}

}

Serialization.java

package ZHANG.IO.Serialization;

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 Serialization {

public static void main(String[] args) {

Student stu1 = new Student(21,"zhangsan",25,"computer");

Student stu2 = new Student(22,"baiyan",22,"huaxue");

try {

FileOutputStream fos = new FileOutputStream("student.txt");

ObjectOutputStream os = new ObjectOutputStream(fos);

os.writeObject(stu1);

os.writeObject(stu2);

os.close();

FileInputStream fis = new FileInputStream("student.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

stu1 = (Student) ois.readObject();

stu2 = (Student) ois.readObject();

System.out.println("student1 details:"+stu1.name+" "+stu1.age+" "+stu1.id+" "+stu1.department);

System.out.println("student2 details:"+stu2.name+" "+stu2.age+" "+stu2.id+" "+stu2.department);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

===========

private transient String name;   //不需要被序列化 加transient=-=-=

另一個列子:

package com.serDemo;

import java.io.Serializable;

public class Person implements Serializable{

private String name; //不需要被序列化 加transient

private int age;

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

@Override

public String toString() {

return "姓名:"+name+",年齡:"+age;

}

}

package com.serDemo;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

public class ObjectoutputStreamDemo {

public static void main(String[] args) throws Exception, IOException {

File file = new File("d://person.ser");

ObjectOutputStream oos = null;

oos = new ObjectOutputStream(new FileOutputStream(file));

Person p = new Person("戰士12",34);

oos.writeObject(p);

oos.close();

}

}

package com.serDemo;

import java.io.File;

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 ObjectInputStreamDemo {

public static void main(String[] args) throws Exception, IOException {

File file = new File("d://person.ser");

ObjectInputStream ois = null;

ois = new ObjectInputStream(new FileInputStream(file));

Person p =(Person)ois.readObject();

System.out.println(p);

}

}

//對一組對象進行序列化

package com.serDemo;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

public class ArraySerDemo {

public static void main(String[] args) throws Exception {

Person[] per = {new Person("王武",33),new Person("王六",44),new Person("司機",55)};

ser(per);

Person p[] = (Person[])dser();

print(p);

}

public static void ser(Object obj) throws Exception{

File file = new File("d://person.ser");

ObjectOutputStream oos = null;

oos = new ObjectOutputStream(new FileOutputStream(file));

oos.writeObject(obj);

oos.close();

}

public static Object dser() throws Exception{

Object temp = null;

File file = new File("d://person.ser");

ObjectInputStream ois = null;

ois = new ObjectInputStream(new FileInputStream(file));

temp =ois.readObject();

return temp;

}

public static void print(Person per[]){

for(Person p : per){

System.out.println(p);

}

}

}