android File存儲對象 File存儲到SD卡

android File存儲對象

1、保存對象到本地或SD卡

[java] view plaincopy
  1.               public void fileSave(OAuthV1 oAuth_1){  
  2.     //保存在本地  
  3.     try {  
  4.         // 通過openFileOutput方法得到一個輸出流,方法參數爲創建的文件名(不能有斜槓),操作模式  
  5.         FileOutputStream fos = this.openFileOutput("oauth_1.out",Context.MODE_WORLD_READABLE);  
  6.         ObjectOutputStream oos = new ObjectOutputStream(fos);  
  7.         oos.writeObject(oAuth_1);// 寫入  
  8.         fos.close(); // 關閉輸出流  
  9.         //Toast.makeText(WebviewTencentActivity.this, "保存oAuth_1成功",Toast.LENGTH_LONG).show();  
  10.     } catch (FileNotFoundException e) {  
  11.         e.printStackTrace();  
  12.         //Toast.makeText(WebviewTencentActivity.this, "出現異常1",Toast.LENGTH_LONG).show();  
  13.     } catch (IOException e) {  
  14.         e.printStackTrace();  
  15.         //Toast.makeText(WebviewTencentActivity.this, "出現異常2",Toast.LENGTH_LONG).show();  
  16.     }  
  17.       
  18.     //保存在sd卡  
  19.     if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
  20.           
  21.            File sdCardDir = Environment.getExternalStorageDirectory();//獲取SDCard目錄  
  22.            File sdFile = new File(sdCardDir, "oauth_1.out");  
  23.              
  24.       
  25.         try {  
  26.             FileOutputStream fos = new FileOutputStream(sdFile);  
  27.             ObjectOutputStream oos = new ObjectOutputStream(fos);  
  28.             oos.writeObject(oAuth_1);// 寫入  
  29.             fos.close(); // 關閉輸出流  
  30.         } catch (FileNotFoundException e) {  
  31.             // TODO Auto-generated catch block  
  32.             e.printStackTrace();  
  33.         } catch (IOException e) {  
  34.             // TODO Auto-generated catch block  
  35.             e.printStackTrace();  
  36.         }  
  37.            //Toast.makeText(WebviewTencentActivity.this, "成功保存到sd卡", Toast.LENGTH_LONG).show();  
  38.              
  39.     }  
  40. }  
需要注意的是,要保存的對象(OAuthV1)一定要實現了Serializable接口。

實現了Serializable接口對象才能被序列化。


2、從本地或SD卡中取出對象

(從本地取得保存的對象)

[java] view plaincopy
  1.               public OAuthV1 readOAuth1() {  
  2. OAuthV1 oAuth_1 = null;  
  3. //String filename = "oauth_file.cfg";  
  4.         try {  
  5.             FileInputStream fis=this.openFileInput("oauth_1.out");   //獲得輸入流  
  6.             ObjectInputStream ois = new ObjectInputStream(fis);  
  7.             oAuth_1 = (OAuthV1)ois.readObject();  
  8.               
  9.             //tv.setText(per.toString());                           //設置文本控件顯示內容  
  10.            // Toast.makeText(ShareTencentActivity.this,"讀取成功",Toast.LENGTH_LONG).show();//彈出Toast消息  
  11.         } catch (StreamCorruptedException e) {  
  12.             //Toast.makeText(ShareTencentActivity.this,"出現異常3",Toast.LENGTH_LONG).show();//彈出Toast消息  
  13.             e.printStackTrace();  
  14.         } catch (OptionalDataException e) {  
  15.             //Toast.makeText(ShareTencentActivity.this,"出現異常4",Toast.LENGTH_LONG).show();//彈出Toast消息  
  16.             e.printStackTrace();  
  17.         } catch (FileNotFoundException e) {  
  18.             //Toast.makeText(ShareTencentActivity.this,"出現異常5",Toast.LENGTH_LONG).show();//彈出Toast消息  
  19.             e.printStackTrace();  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         } catch (ClassNotFoundException e) {  
  23.             //Toast.makeText(ShareTencentActivity.this,"出現異常6",Toast.LENGTH_LONG).show();//彈出Toast消息  
  24.             e.printStackTrace();  
  25.         }  
  26. return oAuth_1;  

(2)從SD卡中取得保存的對象

[java] view plaincopy
  1. public OAuthV1 readOAuth2() {  
  2.     OAuthV1 oAuth_1 = null;  
  3.     File sdCardDir = Environment.getExternalStorageDirectory();//獲取SDCard目錄  
  4.     File sdFile = new File(sdCardDir, "oauth_1.out");  
  5.       
  6.     try {  
  7.         FileInputStream fis=new FileInputStream(sdFile);   //獲得輸入流  
  8.         ObjectInputStream ois = new ObjectInputStream(fis);  
  9.         oAuth_1 = (OAuthV1)ois.readObject();  
  10.         ois.close();  
  11.     } catch (StreamCorruptedException e) {  
  12.         // TODO Auto-generated catch block  
  13.         e.printStackTrace();  
  14.     } catch (OptionalDataException e) {  
  15.         // TODO Auto-generated catch block  
  16.         e.printStackTrace();  
  17.     } catch (FileNotFoundException e) {  
  18.         // TODO Auto-generated catch block  
  19.         e.printStackTrace();  
  20.     } catch (IOException e) {  
  21.         // TODO Auto-generated catch block  
  22.         e.printStackTrace();  
  23.     } catch (ClassNotFoundException e) {  
  24.         // TODO Auto-generated catch block  
  25.         e.printStackTrace();  
  26.     }  
  27.           }  
對SD卡操作別忘了加權限

[html] view plaincopy
  1.         <!-- 在SDCard中創建與刪除文件的權限 -->  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  3. <!-- 往SDCard寫入數據的權限 -->  
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章