Serialization vào Java
Tuần tự hoá trong java xuất xắc serialization trong java là 1 trong những cơ chế nhằm ghi trạng thái của một đối tượng người tiêu dùng vào một byte stream.
Bạn đang xem: Java serializable là gì? serialization và deserialization trong java
Nó chủ yếu được thực hiện trong các technology Hibernate, RMI, JPA, EJB và JMS.
Hoạt động ngược lại của serialization được call là deserialization.
Ưu điểm của Serialization trong java
Nó đa số được sử dụng để truyền trạng thái của đối tượng người tiêu dùng qua mạng (được nghe biết như marshaling).

Java Serialization với array hoặc collection
Trong trường hợp mảng (array) hoặc tập hòa hợp (collection), tất cả các đối tượng người sử dụng của array hoặc collection yêu cầu được tuần trường đoản cú hóa (Serializable). Nếu bất kỳ đối tượng chưa hẳn là serializable, thì quy trình serialization sẽ không còn thành công.
Externalizable trong java
Interface Externalizable hỗ trợ khả năng viết trạng thái của một đối tượng vào một byte stream ở định hình nén. Nó ko phải là 1 giao diện đánh dấu.
Interface Externalizable cung cấp hai phương thức:
public void writeExternal(ObjectOutput out) throws IOExceptionpublic void readExternal(ObjectInput in) throws IOExceptionTừ khóa transient trong java
Nếu không thích serialize bất kỳ thuộc tính nào của một lớp, chúng ta cũng có thể đánh lốt nó với từ khóa transient.
Xem thêm: The Anh Veloc - (Chọn Mẫu)Sticker Decal Dán Xe Lái Xe An Toàn
Ví dụ, khai báo một tấm Student, có ba thành viên dữ liệu là id, name cùng age. Lớp Student implements giao tiếp Serializable, tất cả các giá chỉ trị sẽ được tuần tự nhưng nếu bạn không mong muốn tuần tự cho một giá trị, ví dụ: age thì bạn cũng có thể khai báo age với tự khóa transient.
package com.x-lair.com.serialize;import java.io.Serializable;public class Student implements Serializable private static final long serialVersionUID = -266706354210367639L;private int id;private String name;private transient int age;public Student(int id, String name, int age) this.id = id;this.name = name;this.age = age;
Overridepublic String toString() return "Student
package com.x-lair.com.bytestream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;public class SerializationTransientExample public static void main(String args<>) throws Exception ObjectOutputStream oos = null;try oos = new ObjectOutputStream(new FileOutputStream("data/student.txt"));Student student = new Student(1, "x-lair.com", 28);oos.writeObject(student);oos.flush(); catch (IOException ex) ex.printStackTrace(); finally oos.close();System.out.println("success...");Ví dụ áp dụng ObjectInputStream để đọc đối tượng người sử dụng student tự file
package com.x-lair.com.bytestream;import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;public class DeserializationTransientExample public static void main(String args<>) throws Exception ObjectInputStream ois = null;try ois = new ObjectInputStream(new FileInputStream("data/student.txt"));Student student = (Student) ois.readObject();System.out.println(student); catch (IOException ex) ex.printStackTrace(); finally ois.close();Thực thi chương tình SerializationTransientExample, kế tiếp thực thi lịch trình DeserializationTransientExample. Ta có hiệu quả như sau: