您的位置:首页 > 移动开发 > Android开发

android本地、sd卡保存对象或集合,以及读取该对象

2016-11-02 13:41 513 查看
<!-- 在SDCard中创建与删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


存储

/**
* 将对象保存到本地
* @param context
* @param fileName 文件名
* @param bean  对象
* @return true 保存成功
*/
public boolean writeObjectIntoLocal(Context context,String fileName,T bean){
try {
// 通过openFileOutput方法得到一个输出流,方法参数为创建的文件名(不能有斜杠),操作模式
@SuppressWarnings("deprecation")
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(bean);//写入
fos.close();//关闭输入流
oos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
//Toast.makeText(WebviewTencentActivity.this, "出现异常1",Toast.LENGTH_LONG).show();
return false;
} catch (IOException e) {
e.printStackTrace();
//Toast.makeText(WebviewTencentActivity.this, "出现异常2",Toast.LENGTH_LONG).show();
return false;
}
}
/**
* 将对象写入sd卡
* @param fileName 文件名
* @param bean  对象
* @return true 保存成功
*/
public boolean writObjectIntoSDcard(String fileName,T bean){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File sdCardDir = Environment.getExternalStorageDirectory();//获取sd卡目录
File sdFile  = new File(sdCardDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(sdFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(bean);//写入
fos.close();
oos.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
else {
return false;
}
}
/**
* 将集合写入sd卡
* @param fileName 文件名
* @param list  集合
* @return true 保存成功
*/
public boolean writeListIntoSDcard(String fileName,List<T> list){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File sdCardDir = Environment.getExternalStorageDirectory();//获取sd卡目录
File sdFile  = new File(sdCardDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(sdFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);//写入
fos.close();
oos.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
else {
return false;
}
}
}


读取

/**
* 读取本地对象
* @param context
* @param fielName 文件名
* @return
*/
@SuppressWarnings("unchecked")
public T readObjectFromLocal(Context context,String fielName){
T bean;
try {
FileInputStream fis = context.openFileInput(fielName);//获得输入流
ObjectInputStream ois = new ObjectInputStream(fis);
bean = (T) ois.readObject();
fis.close();
ois.close();
return bean;
} catch (StreamCorruptedException e) {
//Toast.makeText(ShareTencentActivity.this,"出现异常3",Toast.LENGTH_LONG).show();//弹出Toast消息
e.printStackTrace();
return null;
} catch (OptionalDataException e) {
//Toast.makeText(ShareTencentActivity.this,"出现异常4",Toast.LENGTH_LONG).show();//弹出Toast消息
e.printStackTrace();
return null;
} catch (FileNotFoundException e) {
//Toast.makeText(ShareTencentActivity.this,"出现异常5",Toast.LENGTH_LONG).show();//弹出Toast消息
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (ClassNotFoundException e) {
//Toast.makeText(ShareTencentActivity.this,"出现异常6",Toast.LENGTH_LONG).show();//弹出Toast消息
e.printStackTrace();
return null;
}
}
/**
* 读取sd卡对象
* @param fileName 文件名
* @return
*/
@SuppressWarnings("unchecked")
public T readObjectFromSdCard(String fileName){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  //检测sd卡是否存在
T bean;
File sdCardDir = Environment.getExternalStorageDirectory();
File sdFile = new File(sdCardDir,fileName);
try {
FileInputStream fis = new FileInputStream(sdFile);
ObjectInputStream ois = new ObjectInputStream(fis);
bean = (T) ois.readObject();
fis.close();
ois.close();
return bean;
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
else {
return null;
}
}

/**
* 读取sd卡对象
* @param fileName 文件名
* @return
*/
@SuppressWarnings("unchecked")
public List<T> readListFromSdCard(String fileName){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  //检测sd卡是否存在
List<T> list;
File sdCardDir = Environment.getExternalStorageDirectory();
File sdFile = new File(sdCardDir,fileName);
try {
FileInputStream fis = new FileInputStream(sdFile);
ObjectInputStream ois = new ObjectInputStream(fis);
list = (List<T>) ois.readObject();
fis.close();
ois.close();
return list;
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
else {
return null;
}
}

}


补充判断是否存在

// 判断文件是否存在
public boolean fileIsExists(String strFile) {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { // 检测sd卡是否存在
File sdCardDir = Environment.getExternalStorageDirectory();// 获取sd卡目录
File sdFile = new File(sdCardDir, strFile);
if (sdFile.exists()) {
return true;
}
}
} catch (Exception e) {
return false;
}

return false;
}


原文地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: