数据保存形式有多种:
1、数据库;
2、sharedpreferences;
3、文件的形式
4、网络存储方式
5、使用contentProvider
这次聊聊文件的缓存方式。
首先将数据以对象的方式序列化下:
/**
* Created by Administrator on 2015-12-25.
* 基类
*/
public class Feed implements Serializable, Parcelable {
/** 1 (新闻网页);2 、3(新闻图片);4(新闻视频);5 用户产生的数据 */
private String feedType;
/** */
private String feedId;
/** */
private String createTime;
/** 赞的条数 */
private String likeCount;
/** */
private String likedThis;
public String getFeedType() {
return feedType;
}
public void setFeedType(String feedType) {
this.feedType = feedType;
}
public String getFeedId() {
return feedId;
}
public void setFeedId(String feedId) {
this.feedId = feedId;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getLikeCount() {
return likeCount;
}
public void setLikeCount(String likeCount) {
this.likeCount = likeCount;
}
public String getLikedThis() {
return likedThis;
}
public void setLikedThis(String likedThis) {
this.likedThis = likedThis;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.feedType);
dest.writeString(this.feedId);
dest.writeString(this.createTime);
dest.writeString(this.likeCount);
dest.writeString(this.likedThis);
}
public Feed() {
}
protected Feed(Parcel in) {
this.feedType = in.readString();
this.feedId = in.readString();
this.createTime = in.readString();
this.likeCount = in.readString();
this.likedThis = in.readString();
}
public static final Parcelable.Creator<Feed> CREATOR = new Parcelable.Creator<Feed>() {
public Feed createFromParcel(Parcel source) {
return new Feed(source);
}
public Feed[] newArray(int size) {
return new Feed[size];
}
};
}
1、把对象的字节序列化永久保存到硬盘上,通常放在一个文件中;
2、在网络上传送对象的字节序列。
这里是要包对象的字节序列化永久保存到硬盘上。
序列化后就可以写入磁盘了
/**
* Created by Administrator on 2015-12-7.
* 首页Feed
*/
public class FeedListCache {
private Context context;
public FeedListCache(Context context) {
this.context = context;
}
public void saveFeedListCache(List<Feed> list) {
write(getCacheFilePath(),list);
}
public List<Feed> getFeedListCache() {
return read(getCacheFilePath());
}
private String getCacheFilePath() {
return FileUtils.getCachePath(context) + File.separator +
"cache_feed";
}
private void write(String path,List<Feed> list) {
if (list == null) {
return;
}
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeObject(list);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private List<Feed> read(String fileName){
ObjectInputStream ois = null;
List<Feed> data = null;
try {
ois = new ObjectInputStream(new FileInputStream
(fileName));
data = new ArrayList<Feed>();
data = (List<Feed>)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
}