java對象序列化與反序列化的默認(rèn)格式和json格式使用示例
默認(rèn)格式
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包。文檔和下載見官網(wǎng)。
序列化
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);
}
}
相關(guān)文章
java異常處理執(zhí)行順序詳解try catch finally
try catch語句是java語言用于捕獲異常并進(jìn)行處理的標(biāo)準(zhǔn)方式,對于try catch及try catch finally執(zhí)行順序必須有深入的了解2021-10-10
詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑
本文主要介紹了springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
SpringBoot2零基礎(chǔ)到精通之?dāng)?shù)據(jù)庫專項精講
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時也是簡化Spring的一種快速開發(fā)的腳手架,本篇我們來學(xué)習(xí)如何連接數(shù)據(jù)庫進(jìn)行操作2022-03-03
MyBatis Plus實現(xiàn)一對多的查詢場景的三種方法
MyBatis Plus提供了多種簡便的方式來進(jìn)行一對多子查詢,本文主要介紹了MyBatis Plus實現(xiàn)一對多的查詢場景的三種方法,具有一定的參考價值,感興趣的可以了解一下2024-07-07
手把手教你使用IDEA創(chuàng)建多模塊(maven)項目
這篇文章主要給大家介紹了關(guān)于如何使用IDEA創(chuàng)建多模塊(maven)項目的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數(shù)據(jù)問題
RequestParam用于將指定的請求參數(shù)賦值給方法中的形參,可以接受簡單類型屬性,也可以接受對象類型,一般用于GET請求,下面這篇文章主要給大家介紹了關(guān)于Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數(shù)據(jù)問題解決的相關(guān)資料,需要的朋友可以參考下2022-10-10
java實現(xiàn)CSV文件導(dǎo)入與導(dǎo)出功能
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)CSV文件導(dǎo)入與導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
Struts2學(xué)習(xí)筆記(9)-Result配置全局結(jié)果集
這篇文章主要介紹Struts2中使用Result配置全局結(jié)果集的方法,希望能給大家做一個參考。2016-06-06

