时间:2021-05-19
默认格式
复制代码 代码如下:
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);
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
使用XmlSerializer序列化List对象成XML格式序列化成XML格式,和反序列化原格式复制代码代码如下:Listlst=newList();Custo
详解Python序列化Serialize和反序列化Deserialize序列化(serialization)序列化是将对象状态转换为可保持或传输的格式的过程。与
0.前言对象的序列化和反序列化作用就不再赘述,php中序列化的结果是一个php自定义的字符串格式,有点类似json.我们在任何语言中设计对象的序列化和反序列化都
1、Java序列化与反序列化是什么?Java序列化是指把Java对象转换为字节序列的过程,而Java反序列化是指把字节序列恢复为Java对象的过程:序列化:对象
1.Java序列化与反序列化Java序列化是指把Java对象转换为字节序列的过程;而Java反序列化是指把字节序列恢复为Java对象的过程。2.为什么需要序列化