1. 程式人生 > 其它 >Java 序列化&反序列化基礎

Java 序列化&反序列化基礎

技術標籤:javajava

序列化和反序列化

簡述:

Java序列化是指把Java物件轉換為位元組序列的過程,即把一個物件變為二進位制的資料流的一種方法,通過序列化可以方便地實現物件的傳輸和儲存;Java反序列化是指把位元組序列恢復為Java物件的過程。

student.java:

package com.wu.Bean;

import java.io.Serializable;

public class Student implements Serializable{
	private static final long serialVersionUID = -4307074831333148448
L; private String name; private int age; public Student(String name,int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public int getAge
() { return age; } public String toString() { return "姓名:"+name+",年齡:"+age; } }

StudentSerializable.java:

package com.wu.SerializableDemo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import com.wu.
Bean.Student; public class StudentSerializable { private static final String PATH = "C:"+File.separator+"Users"+File.separator+"Administrator" + File.separator+"Desktop"+File.separator+"程式碼集合"+File.separator+"Java"+File.separator+"studentserializable.txt"; public StudentSerializable(String name,Integer age) { Student s = new Student(name,age); File file = new File(PATH); if(!file.exists()) { try { file.createNewFile(); }catch(IOException e) { e.printStackTrace(); } } // student例項序列化過程 try { FileOutputStream fos = new FileOutputStream(file); //建立檔案輸出流物件 ObjectOutputStream obj = new ObjectOutputStream(fos); // 將檔案輸出流物件轉化為物件輸出流物件 obj.writeObject(s); // 將student例項寫入物件輸出流 obj.flush(); // 重新整理緩衝區 obj.close(); // 關閉物件輸出流物件 fos.close(); // 關閉檔案輸出流物件 }catch(IOException e) { e.printStackTrace(); } } }

StudentDeserializable.java:

package com.wu.SerializableDemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import com.wu.Bean.Student;

public class StudentDeserializable {
	private static final String PATH = "C:"+File.separator+"Users"+File.separator+"Administrator"
			+ File.separator+"Desktop"+File.separator+"程式碼集合"+File.separator+"Java"+File.separator+"studentserializable.txt";
	public StudentDeserializable() {
		File file = new File(PATH);
		if(!file.exists()) {
			try {
				file.createNewFile();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		try {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream obj = new ObjectInputStream(fis);
			Student s = (Student) obj.readObject();
			System.out.println(s);
			obj.close();
			fis.close();
		}catch(IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

TestDemo.java:

package com.wu.SerializableDemo;

import org.junit.Test;

public class TestDemo {
	@Test
	public void test() {
		// 序列化測試
		new StudentSerializable("張三",20);
		// 反序列化測試
		new StudentDeserializable();
	}
}

此時在相應的目錄下會生成一個txt檔案,這裡面的內容和class檔案的一樣:

在這裡插入圖片描述

結果:

在這裡插入圖片描述

transient關鍵字

為了保證物件序列化的高效傳輸,就需要防止一些不必要成員屬性的序列化處理

// 對student類的age屬性新增transient關鍵字 
private static final long serialVersionUID = -4307074831333148448L;
private String name; // 此屬性可被序列化
private transient int age; // 此屬性不被序列化

結果:

在這裡插入圖片描述