android File存储对象 File存储到SD卡

来源:互联网 发布:标签设计软件哪种好 编辑:程序博客网 时间:2024/05/21 08:50

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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}            //Toast.makeText(WebviewTencentActivity.this, "成功保存到sd卡", Toast.LENGTH_LONG).show();            }}
需要注意的是,要保存的对象(OAuthV1)一定要实现了Serializable接口。

实现了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 blocke.printStackTrace();} catch (OptionalDataException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}           }
对SD卡操作别忘了加权限

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



原创粉丝点击