我该如何使用
Kryo
对一个对象进行序列化和反序列化呢?我正在使用Kryo 2.23.0
版本。Kryo
对一个对象进行序列化和反序列化呢?我正在使用Kryo 2.23.0
版本。public class KryoTest {
public static void main(String[] args){
Vector3d someObject=new Vector3d(1,2,3);
//serialise object
//try-with-resources used to autoclose resources
try (Output output = new Output(new FileOutputStream("KryoTest.ser"))) {
Kryo kryo=new Kryo();
kryo.writeClassAndObject(output, someObject);
} catch (FileNotFoundException ex) {
Logger.getLogger(KryTest.class.getName()).log(Level.SEVERE, null, ex);
}
//deserialise object
Vector3d retrievedObject=null;
try (Input input = new Input( new FileInputStream("KryoTest.ser"))){
Kryo kryo=new Kryo();
retrievedObject=(Vector3d)kryo.readClassAndObject(input);
} catch (FileNotFoundException ex) {
Logger.getLogger(KryTest.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Retrieved from file: " + retrievedObject.toString());
}
}
所有最新的文档现在已经转移到了Github上;https://github.com/EsotericSoftware/kryo#quickstart
一个简单的版本:
Kryo kryo = new Kryo();
// #### Store to disk...
Output output = new Output(new FileOutputStream("file.bin"));
SomeClass someObject = ...
kryo.writeObject(output, someObject);
output.close();
// ### Restore from disk...
Input input = new Input(new FileInputStream("file.bin"));
SomeClass someObject = kryo.readObject(input, SomeClass.class);
input.close();