Android序列化工具类源码
时间:2014-04-15 10:51 来源: 我爱IT技术网 作者:微风
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Serialize Utils
*
* @author <a href="http://www.52ij.com" target="_blank">Trinea</a> 2012-5-14
*/
public class SerializeUtils {
/**
* deserialization from file
*
* @param filePath
* @return
* @throws RuntimeException if an error occurs
*/
public static Object deserialization(String filePath) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(filePath));
Object o = in.readObject();
in.close();
return o;
} catch (FileNotFoundException e) {
throw new RuntimeException("FileNotFoundException occurred. ", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException occurred. ", e);
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
}
}
}
}
/**
* serialize to file
*
* @param filePath
* @param obj
* @return
* @throws RuntimeException if an error occurs
*/
public static void serialization(String filePath, Object obj) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(filePath));
out.writeObject(obj);
out.close();
} catch (FileNotFoundException e) {
throw new RuntimeException("FileNotFoundException occurred. ", e);
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
}
}
}
}
}
本文来源 我爱IT技术网 http://www.52ij.com/jishu/5043.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
