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; }
原文地址
相关文章推荐
- android本地、sd卡保存对象或集合,以及读取该对象
- Android 选取图片、手机拍照以及保存图片到本地、读取图片
- Android SharedPreferences保存对象以及读取对象信息
- Android 使用LitePal将list(String,对象,集合)数据保存到本地数据库
- Android 从本地SD卡中读取图片以及编辑图片
- Android文件的保存(本地和SD卡)与读取
- Android文件的保存(本地和SD卡)与读取
- Android 读取本地(SD卡)图片
- 把对象内容以二进制流保存到本地,以及获取
- Android截屏相关以及裁图、保存SD卡
- android数据存储读取3:对象保存(对比IOS)
- Android调用原声分享相关的知识以及保存图片读取图片
- android 全局异常的捕获,保存本地以及上传服务器
- Android 获取屏幕截图 和保存到本地的sd卡路径下
- Android中JNI高级应用 - 本地C代码中创建Java对象及本地JNI对象的保存 .
- serialVersionUID的作用 保存对象到本地文件并读取
- android保存图片到本地sd卡
- android网络获取图片并保存在本地和获取手机SD卡中的图片显示到ImageView上及利用代码删除图片
- Android歌词保存到本地及读取所遇到的字符乱码问题及解决
- android添加新的资源文件夹无法识别以及读取外置SD卡解决方案