java序列化與反序列化操作實(shí)例分析
本文實(shí)例分析了java序列化與反序列化操作。分享給大家供大家參考,具體如下:
概述:
Java序列化是指把Java對(duì)象轉(zhuǎn)換為字節(jié)序列的過程;而Java反序列化是指把字節(jié)序列恢復(fù)為Java對(duì)象的過程。
示例代碼:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; public class Test { /** * @param args */ public static void main(String[] args) { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:/objectFile.obj")); Customer customer = new Customer("中國人",23); out.writeObject("你好!"); out.writeObject(new Date()); out.writeObject(customer); out.writeInt(123); List list = new ArrayList(); int i =0 ; while(i<100) { Customer customer2 = new Customer("中國人",i); list.add(customer2); i++; } HashMap hashMap = new HashMap(); hashMap.put("customer", list); out.writeObject(hashMap); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:/objectFile.obj")); System.out.println("obj1= " + (String) in.readObject()); System.out.println("obj2= " + (Date) in.readObject()); Customer obj3 = (Customer) in.readObject(); System.out.println("obj3= " + obj3); int obj4 = in.readInt(); System.out.println("obj4= " + obj4); Object obj5 = in.readObject(); System.out.println(obj5); HashMap hash_map = (HashMap)obj5; List l = (List) hash_map.get("customer"); System.out.println("size: " + l.size()); for(int ii=0; ii<l.size() -1 ; ii++) { Customer c = (Customer)l.get(ii); System.out.println(c.getName()); System.out.println(c.getAge()); } in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } class Customer implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public String getName() { return name; } public int getAge() { return age; } public Customer(String name, int age) { this.name = name; this.age = age; } public String toString() { return "name=" + name + ", age=" + age; } }
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java操作IO對(duì)象流進(jìn)行數(shù)據(jù)的讀寫
這篇文章主要介紹了Java操作IO對(duì)象流進(jìn)行數(shù)據(jù)的讀寫,本文通過例子逐步介紹了java如何操作IO流,和文字解析,需要的朋友可以參考下2021-07-07基于SpringCloud手寫一個(gè)簡(jiǎn)易版Sentinel
SpringCloud Alibaba Sentinel是當(dāng)前最為流行一種熔斷降級(jí)框架,簡(jiǎn)單易用的方式可以快速幫助我們實(shí)現(xiàn)服務(wù)的限流和降級(jí),保證服務(wù)的穩(wěn)定性。2021-05-05深入解析JVM之內(nèi)存結(jié)構(gòu)及字符串常量池(推薦)
Java作為一種平臺(tái)無關(guān)性的語言,其主要依靠于Java虛擬機(jī)——JVM,接下來通過本文給大家介紹JVM之內(nèi)存結(jié)構(gòu)及字符串常量池的相關(guān)知識(shí),需要的朋友可以參考下2020-07-07詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過示例為大家詳細(xì)介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn)
本文主要介紹了Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04java顯示當(dāng)前運(yùn)行時(shí)的參數(shù)(java運(yùn)行參數(shù))
這篇文章主要介紹了java顯示當(dāng)前運(yùn)行時(shí)參數(shù)的示例(java運(yùn)行參數(shù)),需要的朋友可以參考下2014-04-04SpringBoot項(xiàng)目使用jasypt加解密的方法
jasypt是一個(gè)通用的加解密庫,我們可以使用它在配置文件中對(duì)數(shù)據(jù)庫密碼進(jìn)行加密,以確保其安全性,接下來通過本文給大家介紹SpringBoot項(xiàng)目使用jasypt加解密的方法,感興趣的朋友一起看看吧2022-05-05