android File存储对象
1、保存对象到本地或SD卡
- public void fileSave(OAuthV1 oAuth_1){
- //保存在本地
- try {
- // 通过openFileOutput方法得到一个输出流,方法参数为创建的文件名(不能有斜杠),操作模式
- FileOutputStream fos = this.openFileOutput("oauth_1.out",Context.MODE_WORLD_READABLE);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(oAuth_1);// 写入
- fos.close(); // 关闭输出流
- //Toast.makeText(WebviewTencentActivity.this, "保存oAuth_1成功",Toast.LENGTH_LONG).show();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- //Toast.makeText(WebviewTencentActivity.this, "出现异常1",Toast.LENGTH_LONG).show();
- } catch (IOException e) {
- e.printStackTrace();
- //Toast.makeText(WebviewTencentActivity.this, "出现异常2",Toast.LENGTH_LONG).show();
- }
- //保存在sd卡
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
- File sdFile = new File(sdCardDir, "oauth_1.out");
- try {
- FileOutputStream fos = new FileOutputStream(sdFile);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(oAuth_1);// 写入
- fos.close(); // 关闭输出流
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //Toast.makeText(WebviewTencentActivity.this, "成功保存到sd卡", Toast.LENGTH_LONG).show();
- }
- }
实现了Serializable接口对象才能被序列化。
2、从本地或SD卡中取出对象
(从本地取得保存的对象)
- public OAuthV1 readOAuth1() {
- OAuthV1 oAuth_1 = null;
- //String filename = "oauth_file.cfg";
- try {
- FileInputStream fis=this.openFileInput("oauth_1.out"); //获得输入流
- ObjectInputStream ois = new ObjectInputStream(fis);
- oAuth_1 = (OAuthV1)ois.readObject();
- //tv.setText(per.toString()); //设置文本控件显示内容
- // Toast.makeText(ShareTencentActivity.this,"读取成功",Toast.LENGTH_LONG).show();//弹出Toast消息
- } catch (StreamCorruptedException e) {
- //Toast.makeText(ShareTencentActivity.this,"出现异常3",Toast.LENGTH_LONG).show();//弹出Toast消息
- e.printStackTrace();
- } catch (OptionalDataException e) {
- //Toast.makeText(ShareTencentActivity.this,"出现异常4",Toast.LENGTH_LONG).show();//弹出Toast消息
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- //Toast.makeText(ShareTencentActivity.this,"出现异常5",Toast.LENGTH_LONG).show();//弹出Toast消息
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- //Toast.makeText(ShareTencentActivity.this,"出现异常6",Toast.LENGTH_LONG).show();//弹出Toast消息
- e.printStackTrace();
- }
- return oAuth_1;
(2)从SD卡中取得保存的对象
- public OAuthV1 readOAuth2() {
- OAuthV1 oAuth_1 = null;
- File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
- File sdFile = new File(sdCardDir, "oauth_1.out");
- try {
- FileInputStream fis=new FileInputStream(sdFile); //获得输入流
- ObjectInputStream ois = new ObjectInputStream(fis);
- oAuth_1 = (OAuthV1)ois.readObject();
- ois.close();
- } catch (StreamCorruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (OptionalDataException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- <!-- 在SDCard中创建与删除文件的权限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <!-- 往SDCard写入数据的权限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>