/** 一定要单独存到一个列里面 ,内部类不会被正真的序列化,在读取时报错 * 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"/>
将自定义对象存到文件
最新推荐文章于 2018-09-12 09:34:52 发布