@XQF
2017-02-14T05:53:19.000000Z
字数 4389
阅读 1122
java
序列化在java编程思想上还是没有怎么见过,只是在HeadFirstJava上看过一些,不过也是忘得差不多了。现在就来好好的回顾一下。
对象的寿命通常随着生成该对象的程序的终止而终止。但是有时候我们需要将对象的状态保存下来,在需要的时候将对象恢复。我们把对象的这种能记录自己状态以便将来再生的能力叫做对象的持久性。对象通过写出描述自己状态的数值来记录自己,这个过程叫做对象的序列化。
如果一个对象可以被存放在磁盘上,或者可以发送到另外一台机器并存放到存储器或磁盘上,那么这个对象就被称为可序列化的。Java对象序列化不仅保留一个对象的数据,而且**递归保存对象引用的每个对象的数据
java序列化比较简单,通常不需要编写保存和恢复对象状态的定制代码。实现Java.serializable接口的类对象可以转换册成字节流或从字节流恢复,不需要在类中增加任何代码。不过,这个接口不需要我们在里面实现任何方法,实现接口的意义就是贴一个标签。代表该对象是可序列化的。
序列化:
要序列化一个对象必须和一定的对象输入输出流联系起来,通过对象输出流将对象状态保存下来,再通过对象输入流将对象状态恢复
在java.io包中,提供了ObjectInputStream和ObjectOutputStream,将数据流功能扩展至可读写对象。在ObejctInputStream输出流,将数据流功能扩展至可读写对象。在ObjectInputStream中用readObejct()方法可以直接读取一个对象,在ObejctOutputStream中用writeObeject()方法可以直接将对象保存到输出流。
是一个处理流,必须建立在其他节点流的基础上
FileOutputStream fileout=new FileOutputStream("hehe.txt");ObjectOutputStream objectOut=new ObjectOutputStream(fileout);
writeObject()方法将对象写入流中。所有的对象(String和数组)都可以通过这个方法写入。
objectOut.writeObject("hello");objectOut.writeObject(new Date());
构造方法两个:

是一个处理流,必须建立在其他节点流的基础上.对以前使用的ObjectInputStream写入的数据进行反序列化。
FileInputStream fileIn=new FileInputStream("hehe.txt");ObjectInputStream objectIn=new ObjectInputStream(fileIn);
readObject()方法用于从流中读取对象。注意Obejct的强制类型转换
String s=(String)objectIn.readObject(objectIn);Date date=(Date)objectIn.readObject(objectIn);

class Student implements Serializable {private int id;String name;String sex;int age;String department;public Student(int id, String name, String sex, int age, String department) {this.id = id;this.name = name;this.sex = sex;this.age = age;this.department = department;}public String toString() {String string = id + " " + name + " " + sex + " " + age + " " + department;return string;}};public class RuntimeTest {public static void main(String[] args) {Student stu0 = new Student(11, "XQF", "male", 34, "CS");Student stu1 = new Student(11, "WLY", "male", 34, "CS");try {FileOutputStream fileOut = new FileOutputStream("D:\\workspace\\JavaCode\\JavaClassicClass\\src\\com\\exercise\\test\\test.txt");ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);objectOut.writeObject(stu0);objectOut.writeObject(stu1);objectOut.close();FileInputStream fileIn = new FileInputStream("D:\\workspace\\JavaCode\\JavaClassicClass\\src\\com\\exercise\\test\\test.txt");ObjectInputStream objectIn = new ObjectInputStream(fileIn);Student stu2 = (Student) objectIn.readObject();Student stu3 = (Student) objectIn.readObject();System.out.println(stu2);System.out.println(stu3);} catch (Exception e) {e.printStackTrace();}}}
确实实现了功能,但是想看看文件内的具体数据,。以对象为单位写入文件存储怕是根本见不到文件内容的真面目。
串行化(序列化)只能保存对象的非静态变量,不能保存任何的成员方法和静态成员变量。而且序列化保存的只是变量的值,对于变量的任何修饰符都是没有办法保存的。对于某些类型对象,其状态是瞬时的,这样的对象也是没有办法保存状态的,对于这些字段,我们必须使用transient进行说明。
缺省的序列化首先写入数据和类字段信息,然后按照名称的上升排列顺序写入数据。如果要想明确的控制这些数值的写入顺序和写入种类,必须自定义读取数据流的方式,就是在类的定义中重写writeObject()方法和readObject()方法。
class Student implements Serializable {private int id;String name;String sex;int age;String department;public Student(int id, String name, String sex, int age, String department) {this.id = id;this.name = name;this.sex = sex;this.age = age;this.department = department;}public String toString() {String string = id + " " + name + " " + sex + " " + age + " " + department;return string;}private void writeObject(ObjectOutputStream out) {try {out.writeInt(id);out.writeUTF(name);out.writeUTF(sex);out.writeInt(age);out.writeUTF(department);} catch (IOException e) {e.printStackTrace();}}private void readObject(ObjectInputStream in) throws IOException {id = in.readInt();name = in.readUTF();sex = in.readUTF();age = in.readInt();department = in.readUTF();}};public class RuntimeTest {public static void main(String[] args) {Student stu0 = new Student(11, "XQF", "male", 34, "CS");Student stu1 = new Student(11, "WLY", "male", 34, "CS");try {FileOutputStream fileOut = new FileOutputStream("D:\\workspace\\JavaCode\\JavaClassicClass\\src\\com\\exercise\\test\\test.txt");ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);objectOut.writeObject(stu0);objectOut.writeObject(stu1);objectOut.close();FileInputStream fileIn = new FileInputStream("D:\\workspace\\JavaCode\\JavaClassicClass\\src\\com\\exercise\\test\\test.txt");ObjectInputStream objectIn = new ObjectInputStream(fileIn);Student stu2 = (Student) objectIn.readObject();Student stu3 = (Student) objectIn.readObject();System.out.println(stu2);System.out.println(stu3);objectIn.close();} catch (Exception e) {e.printStackTrace();}}}
老实说UTF没有作用呀,文件打开还是乱码,。,。可能本身就看不见。
好像一切都说得通了,这里的序列化的作用使得自定义类对象化身为流,,,,于是可以将流写入文件内保存,同样也可以用于网络上信息的传输。再从文件中取出流转换为对象就可以了。注意字节流