java的IO中提供了一个 ObjectOutputStream对象,其中的方法writeObject()文档如下:
/**
* Write the specified object to the ObjectOutputStream. The class of the
* object, the signature of the class, and the values of the non-transient
* and non-static fields of the class and all of its supertypes are
* written. Default serialization for a class can be overridden using the
* writeObject and the readObject methods. Objects referenced by this
* object are written transitively so that a complete equivalent graph of
* objects can be reconstructed by an ObjectInputStream.
*
* <p>Exceptions are thrown for problems with the OutputStream and for
* classes that should not be serialized. All exceptions are fatal to the
* OutputStream, which is left in an indeterminate state, and it is up to
* the caller to ignore or recover the stream state.
*
* @throws InvalidClassException Something is wrong with a class used by
* serialization.
* @throws NotSerializableException Some object to be serialized does not
* implement the java.io.Serializable interface.
* @throws IOException Any exception thrown by the underlying
* OutputStream.
*/
public final void writeObject(Object obj) throws IOException {
}
简要的翻译就是“将指定的对象写到输出流”。其实这个方法的文档有些误导的意思,下面这个例子我们会发现,这个方法甚至可以直接序列化集合,如List,Array等。
例子引用自http://blog.csdn.net/hdAptechIvan/
首先建立2个对象,
package serialization;
import java.io.Serializable;
class Employee implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -316102412618444933L;
public Employee(String n, double s)
{
name = n;
salary = s;
}
/**
*加薪水
*/
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
public String toString()
{
return getClass().getName()
+ "[name = "+ name
+ ",salary = "+ salary
+ "]";
}
private String name;
private double salary;
}
package serialization;
class Manager extends Employee
{
public Manager(String n, double s)
{
super(n, s);
secretary = null;
}
/**
*设置秘书
*/
public void setSecretary(Employee s)
{
secretary = s;
}
public String toString()
{
return super.toString()
+ "[secretary = "+ secretary
+ "]";
}
//secretary代表秘书
private Employee secretary;
}
然后直接序列化集合到文件,并通过读取文件,修改数据来验证序列化是成功的:
package serialization;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 对象的序列化
*
*/
public class Test
{
public static void main(String[] args)
{
Employee harry = new Employee("Harry Hacker", 50000);
Manager manager1 = new Manager("Tony Tester", 80000);
manager1.setSecretary(harry);
List<Employee> staff = new ArrayList<Employee>(2);
staff.add(harry);
staff.add(manager1);
//数组也可以
// Employee[] staff = new Employee[2];
//
// staff[0] = harry;
// staff[1] = manager1;
try
{
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("employee.dat"));
out.writeObject(staff); //!!!!!!这个方法可以写集合、数组
out.close();
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("employee.dat"));
List<Employee> newStaff = (List<Employee>)in.readObject();
in.close();
/**
*通过harry对象来加薪
*将在secretary上反映出来
*/
newStaff.get(0).raiseSalary(10);
for (int i = 0; i < newStaff.size(); i++)
System.out.println(newStaff.get(i));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}