由于项目需求,需要保存用户登录过的一些配置,当下次登录的时候读取登录过的配置,所以简单的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,就这样可以了

关于android 将对象写入文件以及从文件读取对象的更多相关文章

  1. java中将list、map对象写入文件

    链接地址:http://blog.sina.com.cn/s/blog_4a4f9fb50101p6jv.html     推荐:凤爪女瓜子男怪象该谁反思伦敦房价为什么持续暴涨 × wvqusrtg个 ...

  2. Android将Log写入文件

    为什么要将Log写入文件 运行应用程序的时候,大多数是不会连接着IDE的: 而当应用程序崩溃时,我们需要收集复现步骤,在设备上复现,并进行Debug: 而由于Android手机的多样性,有些问题是某个 ...

  3. python将对象写入文件,以及从文件中读取对象

    原文地址: http://www.voidcn.com/article/p-fqtqpwxp-wo.html 写入文件代码: >>> import sys, shelve >& ...

  4. Java将对象写入文件读出——序列化与反序列化

    Java类中对象的序列化工作是通过ObjectOutputStream和ObjectInputStream来完成的. 写入: File aFile=new File("e:\\c.txt&q ...

  5. C# 如何将对象写入文件

    http://wenku.baidu.com/link?url=QwDRlO1TeoubnmtUOitXXTRa-eZ6QFKvEuyXyzLXD9c0qCRUV5TL9Fq7_HqvxrMcwsAL ...

  6. Java基础之序列化对象——将对象写入到文件中(SerializeObjects)

    控制台程序. 首先定义一个含有任意不同数据类型域的可序列化类: import java.io.Serializable; public class Junk implements Serializab ...

  7. Android中得到布局文件对象有三种方式

    Android中得到布局文件对象有三种方式 第一种,通过Activity对象 View view = Activity对象.getLayoutInflater().inflater(R.layout. ...

  8. File类的特点?如何创建File类对象?Java中如何操作文件内容,什么是Io流Io流如何读取和写入文件?字节缓冲流使用原则?

    重难点提示 学习目标 1.能够了解File类的特点(存在的意义,构造方法,常见方法) 2.能够了解什么是IO流以及分类(IO流的概述以及分类) 3.能够掌握字节输出流的使用(继承体系结构介绍以及常见的 ...

  9. xml文件生成方式一(字符串拼接,将多实体类对象写入xml文件)

    1.xml文件生成,拼接字符串使用StringBuffer或StringBuilder 2.拼接好后写入文件即可,将多个实体类写入xml文件 3.这种方式比较简单,但是操作也比较麻烦 4.下面是我的代 ...

随机推荐

  1. 五分钟solr4.5教程(搭建、运行)

    环境要求 jdk1.6及以上版本 solr发布版本 下载地址 http://lucene.apache.org/solr/mirrors-solr-latest-redir.html? 启动solr ...

  2. 【转】一致性hash算法(consistent hashing)

    consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1  ...

  3. 《Python CookBook2》 第一章 文本 - 测试一个对象是否是类字符串 && 字符串对齐

    测试一个对象是否是类字符串 任务 有时候需要测试一个对象,尤其是当你在写一个函数或者方法的时候,经常需要测试传入的参数是否是一个字符串. 解决方案 利用内建的isinstance 和basestrin ...

  4. netty的入门

    netty是什么? netty是一个基于NIO的通信框架,对于传统计算机,系统的瓶颈一直在输入输出设备上,计算速度超过IO速度,所以对于i o的性能提高异常重要. 什么是NIO? 非阻塞IO,N表示n ...

  5. sql联接那点儿事儿

    1.交叉联接(cross join) select * from t.toy ,b.boy from toy as t cross join boy as b 其效果同select * from t. ...

  6. nodejs学习笔记之mongoDB

    这两天在学习nodejs,但是发现那本书nodejs入门指南上所用的好多方法都报错. 这里主要说下数据库部分 关于注册部分:书上创建数据库那里可能要小心点,用户名不存在的时候,下面调用save的对象要 ...

  7. Tkinter教程之Event篇(1)'

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1823544 ''Tkinter教程之Event篇(1)'''# 事件的使用方法'''1.测试鼠 ...

  8. Tkinter教程之Toplevel篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811341 '''Tkinter教程之Toplevel篇'''#TopLevel与Frame类 ...

  9. JAVA与多线程开发(线程基础、继承Thread类来定义自己的线程、实现Runnable接口来解决单继承局限性、控制多线程程并发)

    实现线程并发有两种方式:1)继承Thread类:2)实现Runnable接口. 线程基础 1)程序.进程.线程:并行.并发. 2)线程生命周期:创建状态(new一个线程对象).就绪状态(调用该对象的s ...

  10. hdfs[命令] dfs

    Usage: hadoop fs [generic options] [-appendToFile <localsrc> ... <dst>] [-cat [-ignoreCr ...