关于android 将对象写入文件以及从文件读取对象

由于项目需求,需要保存用户登录过的一些配置,当下次登录的时候读取登录过的配置,所以简单的SharePreferences没有办法满足,于是找到了Java中ObjectInputStream 与 ObjectOutputStream这两个包装类可用于输入流中读取对象类数据和将对象类型的数据写入到底层输入流 。ObjectInputStream 与 ObjectOutputStream 类所读写的对象必须实现了 Serializable 接口。

 /**
     * 文件转化为Object
     * @param fis
     * @return byte[]
     */
    public static Object file2Object(FileInputStream fis/*String fileName*/) {

        //FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            //fis = new FileInputStream(fileName);
            ois = new ObjectInputStream(fis);
            Object object = ois.readObject();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     *object转化为文件
     * @param obj
     * @param fos
     */
    public static void object2File(Object obj, FileOutputStream fos/*String outputFile*/) {
        ObjectOutputStream oos = null;
        //FileOutputStream fos = null;
        try {
            //fos = new FileOutputStream(new File(outputFile));
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
/**
 * Created by james.li on 13-9-25.
 */
public class LoginUserInfo implements Serializable{
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -6846034858002233878L;

    private String userCC;

    private String userSN;

    private String productKind;

    public LoginUserInfo() {
    }

    public LoginUserInfo(String userCC, String userSN,String productKind) {
        this.userCC = userCC;
        this.userSN = userSN;
        this.productKind = productKind;
    }

    public String getUserCC() {
        return this.userCC;
    }
    public String getUserSN() {
        return this.userSN;
    }
    public String getProductKind() {
        return this.productKind;
    }

    public void setUserCC(String userCC) {
        this.userCC = userCC;
    }

    public void setUserSN(String userSN) {
        this.userSN = userSN;
    }

    public void setProductKind(String productKind) {
        this.productKind = productKind;
    }

    @Override
    public String toString() {
        return "userCC=[ " + userCC + " ] userSN=[ " + userSN + " ] productKind=[ "
                + productKind + "] .";
    }
}

使用

    public static final String USER_LOGIN_INFO = "loginUserInfo.obj";
 try {
            //先读出来
            FileInputStream is = openFileInput(com.launch.rcu.util.CommonUtil.USER_LOGIN_INFO);
            userLoginList = (List<LoginUserInfo>) com.launch.rcu.util.CommonUtil.file2Object(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
//
                FileOutputStream os = null;
                try {
                    os = openFileOutput(CommonUtil.USER_LOGIN_INFO,MODE_PRIVATE);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                CommonUtil.object2File(userLoginList, os);

ok,就这样可以了

转载于:https://www.cnblogs.com/9420sunshine/p/3339276.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值