关于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.下面是我的代 ...
 
随机推荐
- delphi 对话框初始地址InitialDir
			
我的电脑:SaveDialog1.InitialDir := '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}';// My Computer {20D04FE0-3 ...
 - 2014年acm亚洲区域赛·鞍山站
			
今天北京赛站的比赛也结束了···看了一天的直播之后意识到鞍山站的比赛都过去了一个多月了···这一个月比较萎靡···整天都在睡觉写报告画工图中度过··· 鞍山比哈尔滨还是暖和很多的···就是山上有奇怪的 ...
 - HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题
			
分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...
 - bzoj1036: [ZJOI2008]树的统计Count  树链剖分+线段树
			
入门题 + 熟悉代码 /************************************************************** Problem: 1036 User: 96655 ...
 - PHP $_SERVER的详细参数及说明
			
$_SERVER['PHP_SELF']#当前正在执行脚本的文件名,与documentroot相关. $_SERVER['argv']#传递给该脚本的参数. $_SERVER['argc']#包含传递 ...
 - 【原】Storm Tutorial
			
Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...
 - 2.1……Android中的单位简介
			
引用自Google API Guides Dimension A dimension value defined in XML. A dimension is specified with a num ...
 - iOS开发相关图书推荐
			
Objective-C编程之道:iOS设计模式解析 作 者 [美] Carlo Chung 著:刘威 译 出 版 社 人民邮电出版社 出版时间 2011-11-01 版 次 1 页 ...
 - linux vim用法总结
			
1.跳转到指定行 编辑模式下:输入 ngg或nG(n代表行数) 命令模式下:输入 :n(n代表行数) 2.查找命令 命令模式下输入 / 后面加上查找的内容 例如 :/name (查找 ...
 - 个人思考:能否sub.prototye=sup.prototype实现继承
			
var Sup=function(name){ this.name=name; }; var Sub=function(name){ this.name=name; }; Sup.prototype. ...