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"/>