Java 第十二课:Java 序列化

Java序列化
所谓序列化就是将一个对象表示为一个字节序列;对象的数据、类型信息和存储在对象中的数据类型都包含在该字节序列中,对象序列化之后可以反序列化将对象还原。整个序列化和反序列化过程都是由JVM独立完成的,可以实现完全的跨平台操作。
类 ObjectInputStream 和 ObjectOutputStream 是高层次的数据流,它们包含反序列化和序列化对象的方法。

1. 序列化一个对象的方法,并将它发送到输出流:
	public final void writeObject(Object x) throws IOException

2. 从流中取出下一个对象,并将它反序列化:
	public final Object readObject() throws IOException, ClassNotFoundException
	它的返回值为Object,因此,你需要将它转换成合适的数据类型。
  1. 序列化条件:
    1. 该类必须实现 java.io.Serializable 对象。
    2. 该类的所有属性必须是可序列化的。如果有一个属性不是可序列化的,则该属性必须注明是短暂的。
    3. 如果你想知道一个 Java 标准类是否是可序列化的,请查看该类的文档。检验一个类的实例是否能序列化十分简单, 只需要查看该类有没有实现 java.io.Serializable接口。
实例:
	public static class Students implements java.io.Serializable {
		//private Integer Score;
		public String name;
		public String sex;
		public Integer age;
		public String email;
		
		public void showStudentsInfo() {
			//System.out.println(name + sex + age + email);
			System.out.printf("%s %s %d %s." , name, sex, age, email);
		}		
	}
	
	public static void MySerialization() {
		Students stu1 = new Students();
		stu1.name = "lili";
		stu1.sex = "woman";
		stu1.age = 22;
		stu1.email = "[email protected]";
		try {
			FileOutputStream fileOut = new FileOutputStream("/tmp/ser.bin");
			ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
			objOut.writeObject(stu1);
			objOut.close();
			fileOut.close();
			System.out.print("序列化对象完成.\n");
		} catch(IOException e) {
			e.printStackTrace();
		}
		System.out.print("=======================\n");
		
		Students stud2 = new Students();
		try {
			FileInputStream fileIn = new FileInputStream("/tmp/ser.bin");
			ObjectInputStream objIn = new ObjectInputStream(fileIn);
			stud2 = (Students)objIn.readObject();
			objIn.close();
			fileIn.close();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} catch (ClassNotFoundException c) {
			c.printStackTrace();
			return ;
		}
		
		System.out.printf("%s %s %d %s\n", stud2.name, stud2.sex, stud2.age, stud2.email);
		System.out.printf("反序列化完成呢个.\n");
	}
	输出结果:
		序列化对象完成.
		=======================
		lili woman 22 [email protected]
		反序列化完成呢个.

查看/tmp/ser.bin
hexdump -C /tmp/ser.bin
00000000  ac ed 00 05 73 72 00 18  6c 65 73 73 6f 6e 33 2e  |....sr..lesson3.|
00000010  4c 65 73 73 6f 6e 33 24  53 74 75 64 65 6e 74 73  |Lesson3$Students|
00000020  83 c6 40 19 a9 c3 f1 b8  02 00 04 4c 00 03 61 67  |[email protected]|
00000030  65 74 00 13 4c 6a 61 76  61 2f 6c 61 6e 67 2f 49  |et..Ljava/lang/I|
00000040  6e 74 65 67 65 72 3b 4c  00 05 65 6d 61 69 6c 74  |nteger;L..emailt|
00000050  00 12 4c 6a 61 76 61 2f  6c 61 6e 67 2f 53 74 72  |..Ljava/lang/Str|
00000060  69 6e 67 3b 4c 00 04 6e  61 6d 65 71 00 7e 00 02  |ing;L..nameq.~..|
00000070  4c 00 03 73 65 78 71 00  7e 00 02 78 70 73 72 00  |L..sexq.~..xpsr.|
00000080  11 6a 61 76 61 2e 6c 61  6e 67 2e 49 6e 74 65 67  |.java.lang.Integ|
00000090  65 72 12 e2 a0 a4 f7 81  87 38 02 00 01 49 00 05  |er.......8...I..|
000000a0  76 61 6c 75 65 78 72 00  10 6a 61 76 61 2e 6c 61  |valuexr..java.la|
000000b0  6e 67 2e 4e 75 6d 62 65  72 86 ac 95 1d 0b 94 e0  |ng.Number.......|
000000c0  8b 02 00 00 78 70 00 00  00 16 74 00 10 6c 69 6c  |....xp....t..lil|
000000d0  69 40 73 69 6e 61 2e 63  6f 6d 2e 63 6e 74 00 04  |[email protected]..|
000000e0  6c 69 6c 69 74 00 05 77  6f 6d 61 6e              |lilit..woman|
000000ec

猜你喜欢

转载自blog.csdn.net/qq_39440596/article/details/88535397
今日推荐