关于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,就这样可以了
关于android 将对象写入文件以及从文件读取对象的更多相关文章
- java中将list、map对象写入文件
链接地址:http://blog.sina.com.cn/s/blog_4a4f9fb50101p6jv.html 推荐:凤爪女瓜子男怪象该谁反思伦敦房价为什么持续暴涨 × wvqusrtg个 ...
- Android将Log写入文件
为什么要将Log写入文件 运行应用程序的时候,大多数是不会连接着IDE的: 而当应用程序崩溃时,我们需要收集复现步骤,在设备上复现,并进行Debug: 而由于Android手机的多样性,有些问题是某个 ...
- python将对象写入文件,以及从文件中读取对象
原文地址: http://www.voidcn.com/article/p-fqtqpwxp-wo.html 写入文件代码: >>> import sys, shelve >& ...
- Java将对象写入文件读出——序列化与反序列化
Java类中对象的序列化工作是通过ObjectOutputStream和ObjectInputStream来完成的. 写入: File aFile=new File("e:\\c.txt&q ...
- C# 如何将对象写入文件
http://wenku.baidu.com/link?url=QwDRlO1TeoubnmtUOitXXTRa-eZ6QFKvEuyXyzLXD9c0qCRUV5TL9Fq7_HqvxrMcwsAL ...
- Java基础之序列化对象——将对象写入到文件中(SerializeObjects)
控制台程序. 首先定义一个含有任意不同数据类型域的可序列化类: import java.io.Serializable; public class Junk implements Serializab ...
- Android中得到布局文件对象有三种方式
Android中得到布局文件对象有三种方式 第一种,通过Activity对象 View view = Activity对象.getLayoutInflater().inflater(R.layout. ...
- File类的特点?如何创建File类对象?Java中如何操作文件内容,什么是Io流Io流如何读取和写入文件?字节缓冲流使用原则?
重难点提示 学习目标 1.能够了解File类的特点(存在的意义,构造方法,常见方法) 2.能够了解什么是IO流以及分类(IO流的概述以及分类) 3.能够掌握字节输出流的使用(继承体系结构介绍以及常见的 ...
- xml文件生成方式一(字符串拼接,将多实体类对象写入xml文件)
1.xml文件生成,拼接字符串使用StringBuffer或StringBuilder 2.拼接好后写入文件即可,将多个实体类写入xml文件 3.这种方式比较简单,但是操作也比较麻烦 4.下面是我的代 ...
随机推荐
- 给IT新男的15点建议:苦逼程序员的辛酸反省与总结
很多人表面上看着老实巴交的,实际上内心比谁都好强.自负.虚荣.甚至阴险.工作中见的多了,也就习惯了. 有一些人,什么事都写在脸上,表面上经常得罪人,甚至让人讨厌.但是他们所表现的又未必不是真性情. 我 ...
- CMake实践(2)
一,本期目标: [~@localhost t2]$ cat README this is README├── CMakeLists.txt├── COPYRIGHT├── doc│ └── hel ...
- 将spfile存储在ASM中
数据库的spfile开始是存储在普通的文件系统中,如下所示: SQL> show parameter spfile NAME TYPE VALUE ----------------------- ...
- 在ubuntu下安装chrome
To add PPA in Ubuntu 14.04 / 13.10 / 13.04 / 12.10 / 12.04 First download and install the key from G ...
- oc_转_NSArrray 和 NSMutableArrray
Objective C 中除了可以使用C中的基本数组外,如 int[5];,char word[] = {‘a’, 'b’, 'c’};,Foundation 还提供了 NSArray 类.Found ...
- 如何用chrome修改js代码,跳过网站等待时间
用chrome修改js代码 By Z.H. Fu 切问录 [maplewizard.github.io](http://maplewizard.github.io ) 网页中大部分的限制都是由js编写 ...
- <Chapter 2>2-2.开发应用(developing the Application)
一个App Engine应用对网络请求做出响应.它是通过调用请求处理器(quest handlers)来实现的,接受请求参数并返回响应的程序.对于来自请求URL上的请求,App Engine通过一个配 ...
- 【hadoop代码笔记】hadoop作业提交之汇总
一.概述 在本篇博文中,试图通过代码了解hadoop job执行的整个流程.即用户提交的mapreduce的jar文件.输入提交到hadoop的集群,并在集群中运行.重点在代码的角度描述整个流程,有些 ...
- Codeforces 381 简要题解
做的太糟糕了...第一题看成两人都取最优策略,写了个n^2的dp,还好pre-test良心(感觉TC和CF的pretest还是很靠谱的),让我反复过不去,仔细看题原来是取两边最大的啊!!!前30分钟就 ...
- 深入浅出Zookeeper
能找到的一些zookeeper的资料一上来不是扯一通paxos算法就是一大坨一大坨的代码.很多人对zookeeper更多的是听过,所以这一篇文章就尝试用尽可能用精简的语言科普zookeeper. zo ...