激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java對象序列化與反序列化的默認格式和json格式使用示例

java對象序列化與反序列化的默認格式和json格式使用示例

2019-11-08 14:25java教程網 JAVA教程

這篇文章主要介紹了java對象序列化與反序列化的默認格式和json格式使用示例,需要的朋友可以參考下

默認格式

 

復制代碼代碼如下:

public class MyClass implements Serializable{
...}

 

序列化

復制代碼代碼如下:

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

 

反序列化

復制代碼代碼如下:

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

 

JSON格式

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

序列化

復制代碼代碼如下:

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

 

反序列化:

復制代碼代碼如下:

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

 

完整測試代碼

復制代碼代碼如下:


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);
    }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品一区2区3区 | 国产成人在线一区 | 爱唯侦察 国产合集 亚洲 | 久久精品成人影院 | 久草在线视频中文 | 日本最新免费二区三区 | 黄色片免费在线播放 | 欧美一级电影网站 | 日韩av有码在线 | 日本黄网| 91网视频在线观看 | 国产宾馆3p国语对白 | 777zyz色资源站在线观看 | 在线看毛片的网站 | 久久激情小视频 | 9191久久久久视频 | 免费国产一级淫片 | 精品久久一区二区 | 国产一国产一级毛片视频在线 | 成人小视频在线播放 | 黄色av免费电影 | 激情综合婷婷久久 | 国产99视频在线观看 | 亚州成人在线观看 | 国产人成免费爽爽爽视频 | 麻豆视频网 | 欧美一级理论 | 久久久久久久久久久国产精品 | 一级黄色欧美 | 激情网站免费观看 | 男男羞羞视频网站国产 | 欧美激情精品久久久久久久久久 | h视频免费在线观看 | 国产免费黄网 | 男人午夜视频 | 蜜桃视频观看麻豆 | 色妞视频男女视频 | 色播久久 | 亚洲第一色片 | 亚洲成人夜色 | 好吊色欧美一区二区三区四区 |