import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.*;
public class HelloKryo {
static public void main (String[] args) throws Exception {
Kryo kryo = new Kryo();
kryo.register(SomeClass.class); //将序列化的类注册到Kryo
SomeClass object = new SomeClass();
object.value = "Hello Kryo!";
Output output = new Output(new FileOutputStream("file.bin")); //创建输出流Output
kryo.writeObject(output, object); //序列化
output.close();
Input input = new Input(new FileInputStream("file.bin")); //创建输入流Input
SomeClass object2 = kryo.readObject(input, SomeClass.class); //反序列化
input.close();
}
static public class SomeClass {
String value;
}
注意Kryo是非线程安全的,如果多个线程同时进行序列化过程,可将Kryo存放至ThreadLoc