将自定义对象存到文件



/** 一定要单独存到一个列里面   ,内部类不会被正真的序列化,在读取时报错
 * Created by zyh on 2016/12/22.
 */

public class Person implements Serializable {
    private static final long serialVersionUID = 5750934782325972L;
    String name, phone, address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public Person(String name, String phone, String address) {
        this.name = name;
        this.phone = phone;
        this.address = address;
    }
}




/**
 * Created by zyh on 2016/12/22.
 */

public class TestWriteDataSDcard extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Person person = new Person("zyh", "18813155449", "北京市海淀区");

//保存对象到本地
        saveObject("object.dat", person);


//得到保存于本地路径的对象
        new Handler().postDelayed(new Runnable() {
                                      @Override
                                      public void run() {
//                                          Person p = readOAuth2();
                                          Person p = (Person) getObject("object.dat");

                                          Log.e("after", "   获取数据      " + p.toString());
                                      }
                                  }, 2000
        );

    }

    /**
     * 保存文件
     *
     * @param name
     * @param person
     */
    private void saveObject(String name, Person person) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = this.openFileOutput(name, MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(person);
        } catch (Exception e) {
            e.printStackTrace();
            //这里是保存文件产生异常
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    //fos流关闭异常
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    //oos流关闭异常
                    e.printStackTrace();
                }
            }
        }

        //sdk
        //保存在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(person);// 写入
//                fos.close(); // 关闭输出流
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            Toast.makeText(this, "成功保存到sd卡", Toast.LENGTH_LONG).show();
//
//        }
    }

    /**
     * 从sd卡中读出
     * @return
     */
    public Person readOAuth2() {
        Person 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 = (Person) 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();
        }
        return oAuth_1;
    }

    /**
     * 读取文件  可用
     *
     * @param name 文件名?
     * @return
     */
    private Object getObject(String name) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = this.openFileInput(name);
            ois = new ObjectInputStream(fis);
            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            //这里是读取文件产生异常
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    //fis流关闭异常
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    //ois流关闭异常
                    e.printStackTrace();
                }
            }
        }
        //读取产生异常,返回null
        return null;
    }

}
权限
<!-- SD卡创建和删除文件 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- SD卡读写 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值