存储序列化对象
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream ( baos) ;
out. writeObject ( 需要存储的对象) ;
String objectVal = new String ( Base64. encode ( baos. toByteArray ( ) , Base64. DEFAULT) ) ;
spCache. put ( KEY, objectVal) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( baos != null) {
baos. close ( ) ;
}
if ( out != null) {
out. close ( ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
取出序列化对象
String json = spCache. get ( KEY, "" ) ;
byte [ ] buffer = Base64. decode ( json, Base64. DEFAULT) ;
ByteArrayInputStream bais = new ByteArrayInputStream ( buffer) ;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream ( bais) ;
views = ( List< TextView> ) ois. readObject ( ) ;
} catch ( IOException | ClassNotFoundException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( bais != null) {
bais. close ( ) ;
}
if ( ois != null) {
ois. close ( ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}