一、對象的序列化和反序列化
1、對象的序列化和反序列化
/** * 將對象序列化到文件中 * * @param obj 對象 * @param file 文件 * @throws IOException IO異常 */public static void writeObjectToFile(Object obj, File file) throws IOException { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { oos.writeObject(obj); }}/** * 從文件中反序列化得到對象 * * @param file 文件 * @param 對象類型 * @return 反序列化得到的對象 * @throws IOException IO異常 * @throws ClassNotFoundException 找不到類異常 */public static T readObjectFromFile(File file) throws IOException, ClassNotFoundException { try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { return (T) ois.readObject(); }}
2、對象的序列化和反序列化例子
public class SerializeObject { public static void main(String[] args) throws IOException, ClassNotFoundException { File file = new File("object.txt"); Student studentSrc = new Student("Tom", 18); writeObjectToFile(studentSrc, file); Student studentDst = readObjectFromFile(file); System.out.println("studentDst = " + studentDst); }}class Student implements Serializable { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; }}
二、ObjectInputStream類的詳解
1、ObjectInputStream類簡介
public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants { protected ObjectInputStream() throws IOException, SecurityException {} public ObjectInputStream(InputStream in) throws IOException { verifySubclass(); bin = new BlockDataInputStream(in); handles = new HandleTable(10); vlist = new ValidationList(); } private final BlockDataInput bin; // more code omitted}
2、ObjectInputStream類的方法
/** *從流中讀取一個Object對象 * * @throws IOException IO異常 * @throws ClassNotFoundException 找不到類異常 */public final Object readObject() throws IOException, ClassNotFoundException { /* more code */ }/** * 從流中讀取一個boolean類型的值 * * @throws IOException IO異常 */public boolean readBoolean() throws IOException { /* more code */ }
三、ObjectOutputStream類及其相關類的詳解
1、ObjectOutputStream類簡介
public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants { /** 寫入對象 */ public void writeObject(Object obj) throws IOException {...} /** 獲取包含此流中的所有對象曾經的引用 */ protected ObjectStreamClass getObjectStreamClass(Class cl) throws IOException, ClassNotFoundException {...} /** 寫入單個位元組 */ public void write(int val) throws IOException {...} /** 實現數據寫入底層的操作系統 */ public void flush() throws IOException {...} /** 關閉對象輸出流 */ public void close() throws IOException {...}}final class BlockDataOutputStream {...}final class BlockDataInputStream {...}
2、ObjectOutputStream類的方法
/** * 將指定的基本類型寫入流中。 */public final void writeInt(int val) throws IOException { /* more code */ }/** * 將單個double精度浮點數寫入流中 */public final void writeDouble(double val) throws IOException { /* more code */ }/** * 寫入一個UTF-8編碼格式的字符串 */public final void writeUTF(String str) throws IOException { /* more code */ }
四、常用工具類的使用
1、IOUtils類
/** * 將InputStream轉為byte[] * * @param inputStream 輸入流 * @return byte[] * @throws IOException IO異常 */public static byte[] toByteArray(InputStream inputStream) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 4]; int n; while (-1 != (n = inputStream.read(buffer))) { byteArrayOutputStream.write(buffer, 0, n); } return byteArrayOutputStream.toByteArray();}
2、ByteArrayUtils類
/** * 將序列化位元組數組轉為指定類型的對象 * * @param bytes 序列化位元組數組 * @param clazz 指定類型 * @param 類型 * @return 對象 * @throws IOException IO異常 * @throws ClassNotFoundException 找不到類異常 */public static T deserialize(byte[] bytes, Class clazz) throws IOException, ClassNotFoundException { try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis)) { return (T) ois.readObject(); }}/** * 將對象序列化為位元組數組 * * @param obj 對象 * @return 序列化位元組數組 * @throws IOException IO異常 */public static byte[] serialize(Object obj) throws IOException { try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos)) { oos.writeObject(obj); return bos.toByteArray(); }}
五、總結
本文主要介紹了對象的序列化和反序列化知識及其相關類,同時也介紹了常用工具類的使用。通過學習,我們了解了對象的序列化和反序列化的原理和方法,了解了ObjectInputStream、ObjectOutputStream、BlockDataOutputStream、BlockDataInputStream等相關類的詳細信息,以及IOUtils和ByteArrayUtils這兩個常用的工具類的使用方法。希望對你的學習和工作有所幫助。
原創文章,作者:QXTZU,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/332014.html