欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java對象序列化與反序列化的默認(rèn)格式和json格式使用示例

 更新時間:2014年02月21日 15:16:35   作者:  
這篇文章主要介紹了java對象序列化與反序列化的默認(rèn)格式和json格式使用示例,需要的朋友可以參考下

默認(rèn)格式

復(fù)制代碼 代碼如下:

public class MyClass implements Serializable{
...}

序列化:

復(fù)制代碼 代碼如下:

ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(outputPath));
output.writeObject(myObject);

反序列化:

復(fù)制代碼 代碼如下:

ObjectInputStream input = new ObjectInputStream(new FileInputStream(inputPath));
return (MyClass)input.readObject();

JSON格式

使用jackson包。jackson是一個效率非常高的Java JSON包。文檔和下載見官網(wǎng)。

序列化

復(fù)制代碼 代碼如下:

ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File(outputPath), myObject);

反序列化:

復(fù)制代碼 代碼如下:

return mapper.readValue(new File(outputPath), MyClass.class);

完整測試代碼

復(fù)制代碼 代碼如下:

import java.io.File;
import java.io.FileInputStream;
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 com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Zoo implements Serializable {

    private static final long serialVersionUID = 1L;
    private static ObjectMapper mapper = new ObjectMapper();

    public static int maxAnimalCount;
    public ArrayList<String> animals;

    public Zoo() {
        animals = new ArrayList<String>();
    }

    public static void setMax(int max){
        maxAnimalCount = max;
    }

    /**
     * Add an animal to animals Array.
     * @param animalName
     */
    public void addAnimal(String animalName){
        if (animals.size() < maxAnimalCount)
            animals.add(animalName);
    }

    @Override
    public String toString(){
        return "Zoo: \n animals: " + animals.toString() +
                "\n maxAnimalCount: " + maxAnimalCount + "\n";
    }

    /**
     * Output standard serialization to file at logPath.
     * @param logPath
     */
    public void serializeToLog(String logPath) {
        ObjectOutputStream output = null;
        try
        {
            output = new ObjectOutputStream(
                    new FileOutputStream(logPath));
            output.writeObject(this);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Output JSON serialization(using jackson) to file at logPath.
     * @param logPath
     */
    public void serializeJSONToLog(String logPath){

        try {
            mapper.writeValue(new File(logPath), this);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Standard deserialize a Zoo instance from file at logPath.
     * @param logPath
     * @return deserialized zoo instance
     */
    public static Zoo deserializeFromLog(String logPath) {
        ObjectInputStream input = null;
        try
        {
            input =new ObjectInputStream(
                    new FileInputStream(logPath));
            return (Zoo)input.readObject();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    /**
     * JSON deserialize a Zoo instance from file at logPath.
     * @param logPath
     * @return JSON deserialized zoo instance
     */
    public static Zoo deserializeJSONFromLog(String logPath){
        try {
            return mapper.readValue(new File(logPath), Zoo.class);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

class ZooSerializeTest {
    public static void main(String[] args) {
        Zoo zoo1 = new Zoo();
        Zoo.setMax(100);
        zoo1.addAnimal("hamster");
        zoo1.addAnimal("sheep");

        zoo1.serializeToLog("zoo1.log");

        Zoo zoo2 = new Zoo();
        Zoo.setMax(200);
        zoo2.addAnimal("tiger");

        zoo2.serializeToLog("zoo2.log");

        Zoo.setMax(300);

        //Deserialization
        zoo1 = Zoo.deserializeFromLog("zoo1.log");
        zoo2 = Zoo.deserializeFromLog("zoo2.log");

        System.out.println("zoo1: \n" + zoo1);
        System.out.println("zoo2: \n" + zoo2);

        //Serialize to JSON
        zoo1.serializeJSONToLog("zoo1.json");
        zoo1 = Zoo.deserializeJSONFromLog("zoo1.json");

        System.out.println("zoo1 from json: \n" + zoo1);
    }
}

相關(guān)文章

  • 解決springboot錯誤:找不到或無法加載主類(配置編碼或者M(jìn)aven)

    解決springboot錯誤:找不到或無法加載主類(配置編碼或者M(jìn)aven)

    這篇文章主要介紹了解決springboot錯誤:找不到或無法加載主類(配置編碼或者M(jìn)aven)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Springboot 如何指定獲取出 yml文件里面的配置值

    Springboot 如何指定獲取出 yml文件里面的配置值

    這篇文章主要介紹了Springboot 如何指定獲取出 yml文件里面的配置值操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 詳解MybatisPlus中@Version注解的使用

    詳解MybatisPlus中@Version注解的使用

    在MyBatisPlus中,常常使用@Version實現(xiàn)樂觀鎖,該注解用于字段上面。本文將通過示例詳細(xì)講解@Version注解的使用,感興趣的可以了解一下
    2022-06-06
  • 詳解關(guān)于IntelliJ IDEA中Schedule for Addition 的問題

    詳解關(guān)于IntelliJ IDEA中Schedule for Addition 的問題

    本篇文章主要介紹了詳解關(guān)于 IntelliJ IDEA 中 Schedule for Addition 的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)

    JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)

    這篇文章主要為大家介紹了JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Java虛擬機JVM性能優(yōu)化(一):JVM知識總結(jié)

    Java虛擬機JVM性能優(yōu)化(一):JVM知識總結(jié)

    這篇文章主要介紹了Java虛擬機JVM性能優(yōu)化(一):JVM知識總結(jié),本文是系列文章的第一篇,后續(xù)篇章請繼續(xù)關(guān)注腳本之家,需要的朋友可以參考下
    2014-09-09
  • java實現(xiàn)簡單注冊選擇所在城市

    java實現(xiàn)簡單注冊選擇所在城市

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)簡單注冊選擇所在城市的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • 在idea中為注釋標(biāo)記作者日期操作

    在idea中為注釋標(biāo)記作者日期操作

    這篇文章主要介紹了在idea中為注釋標(biāo)記作者日期操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 詳解java中controller層是干什么的

    詳解java中controller層是干什么的

    Controller一般指的是MVC架構(gòu)里的控制層,是對項目里的功能做統(tǒng)一的調(diào)度,下面這篇文章主要給大家介紹了關(guān)于java中controller層是干什么的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • java排序去重示例分享

    java排序去重示例分享

    這篇文章主要介紹了java排序去重示例,對String strs = "ZZZ BBB AAA OOO ZZZ AAA ZZZ"計算出現(xiàn)個數(shù),排序去重,需要的朋友可以參考下
    2014-02-02

最新評論