/**
* 保存对像
* @param context
* @param fileName
* @param object
*/
public static void saveObject(Context context, String fileName, Object object){
try {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取对像
* @param context
* @param fileName
* @return
*/
public static Object readObject(Context context, String fileName){
Object obj = null;
try {
FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
obj = ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return obj;
}
* 保存对像
* @param context
* @param fileName
* @param object
*/
public static void saveObject(Context context, String fileName, Object object){
try {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取对像
* @param context
* @param fileName
* @return
*/
public static Object readObject(Context context, String fileName){
Object obj = null;
try {
FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
obj = ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return obj;
}
本文介绍了如何在Android应用中使用文件系统实现对象的持久化存储,并提供了对象读取的方法。
276

被折叠的 条评论
为什么被折叠?



