关于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.下面是我的代 ...
随机推荐
- Dev gridControl 按回车增加一行
将NewItemRowPosition属性设置为Top或Bottom, 在这样的新行中输入数据后,会自动添加到绑定的数据源中的, 如果你希望在按回车时焦点跳至下一列, 只需要设置GridView的Op ...
- C# delegate 学习 (练这么久终于悟出来点东东了,继续加油! ^_^)
前言 从事开发工作两年有余了,但还是对Delegate,Event神马的看见就头疼,文章看过无数,自己也练习过好多遍,但到用的时候或者人家换了一种形式之后就又不懂了,哎~智商捉急啊!! 但是,这两天的 ...
- codedorces 260 div2 A题
水题,扫描一遍看是否出现价格低质量高的情况. #include<cstdio> #include<string> #include<vector> #include ...
- Chrome的网络调试
F12 然后
- NGUI学习笔记-Label
属性说明 Overflow: ShrinkContent : 如果文本超出文本框宽度,会自动缩小文本size,使其显示完整 ClampContent : 文本大小固定,超出文本框的部分不会显示,也不会 ...
- bzoj 1408 [Noi2002]Robot(欧拉函数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1408 [题意] 求m的所有约数中,满足可以分解成(奇数个不同素数/偶数个不同素数/其 ...
- Python闭包与javascript闭包比较
实例一 python def line_conf(): def line(x): return 2*x+1 print(line(5)) # within the scope line_con ...
- cocos2d-x图片变灰或者变亮
//根据现有CCSprite,变亮和变灰 CCSprite* FlyLeaf::graylightWithCCSprite(CCSprite* oldSprite,bool isLight) { ...
- work8
使用裸指针: #include <iostream>#include <memory>#include <stdio.h>#include <cstring& ...
- hive UDF函数
虽然Hive提供了很多函数,但是有些还是难以满足我们的需求.因此Hive提供了自定义函数开发 自定义函数包括三种UDF.UADF.UDTF UDF(User-Defined-Function) ...