zoukankan      html  css  js  c++  java
  • File存对象--android 的File存储到SD卡();

     方法1:android File存对象--File存储到SD卡();

    1、保存对象到本地或SD卡
    需要注意的是,要保存的对象(OAuthV1)一定要实现了Serializable接口。
    实现了Serializable接口对象才能被序列化。
    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) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    //Toast.makeText(WebviewTencentActivity.this, "成功保存到sd卡", Toast.LENGTH_LONG).show();

    }
    }


    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();
    } catch (StreamCorruptedException e) {

    e.printStackTrace();
    } catch (OptionalDataException e) {

    e.printStackTrace();
    } catch (FileNotFoundException e) {

    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {

    e.printStackTrace();
    }
    return oAuth_1;

     

    (2)从SD卡中取得保存的对象

    对SD卡操作别忘了加权限
    <!-- 在SDCard中创建与删除文件的权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 往SDCard写入数据的权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    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();
    }
    }

  • 相关阅读:
    XX宝面试题——css部分
    XX宝面试题——JS部分
    Struts、JSTL标签库的基本使用方法
    JavaScript:学习笔记(10)——XMLHttpRequest对象
    SpringBoot学习笔记:单元测试
    SpringMVC:学习笔记(11)——依赖注入与@Autowired
    SpringBoot学习笔记:动态数据源切换
    Django:学习笔记(9)——视图
    Django RF:学习笔记(8)——快速开始
    CNN学习笔记:批标准化
  • 原文地址:https://www.cnblogs.com/awkflf11/p/6024522.html
Copyright © 2011-2022 走看看