Serializable 对象文件存储读取
2013-06-27 16:04
393 查看
网上很多资料,自己写了一份,备份留档用
/* * Copyright (c) 2013 NeuLion, Inc. All Rights Reserved. */ package com.neulion.android.nfl.library.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.content.Context; import android.os.Environment; import com.neulion.android.common.connection.exception.ConnectionException; import com.neulion.android.common.connection.exception.NotFoundException; import com.neulion.android.common.parser.CommonParser; import com.neulion.android.common.parser.exception.ParserException; import com.neulion.android.framework.application.config.ConfigManager; import com.neulion.android.nfl.library.bean.messaging.AppMessages; public class NFLMessageHelper { // private static final String KEY = "NFLMessageUtil"; private static final long ONE_DAY = 24 * 60 * 60 * 1000; public static AppMessages getNFLMessage(Context context) { final String filePath = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/" + "app_message.dat"; final File file = new File(filePath); AppMessages appMsg = null; if (!file.exists()) { try { file.createNewFile(); appMsg = getRemoteAppMessages(context); saveAppMessage(file, appMsg); } catch (Exception e) { e.printStackTrace(); } return appMsg; } ObjectInputStream in = null; try { in = new ObjectInputStream(new FileInputStream(file)); appMsg = (AppMessages) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } final long currentStamp = System.currentTimeMillis(); if (appMsg == null || (currentStamp - appMsg.getStamp()) >= ONE_DAY) { try { appMsg = getRemoteAppMessages(context); saveAppMessage(file, appMsg); } catch (Exception e) { e.printStackTrace(); } } return appMsg; } private static AppMessages getRemoteAppMessages(Context context) throws ConnectionException, NotFoundException, ParserException { final String deviceType = DeviceTypeHelper.isPhone(context) ? "triggerRules_phone" : "triggerRules_tablet"; final String url = ConfigManager.getValue(deviceType); return CommonParser.parse(url, new AppMessages()); } private static void saveAppMessage(File file, AppMessages appMsg) throws FileNotFoundException, IOException { final ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(file)); appMsg.setStamp(System.currentTimeMillis()); out.writeObject(appMsg); out.close(); } }
相关文章推荐
- 存储和读取文件中的自定义对象
- python数据存储系列教程——python对象与json字符串的相互转化,json文件的存储与读取
- plist文件的 偏好设置 存储与读取 自定义对象归档
- C#利用MySQL的BLOB存储和读取文件和对象
- 将实现Serializable接口的对象存放进文件中,然后读取出来
- 文件中的每一行存储在 vector容器对象中,读取每行的单词
- 数据库中存储与读取文件
- axis1.4部署webService 读取和响应Header信息\传送文件\BasicHandler\ SimpleChain\传递对象集合等测试
- 数据库中存储与读取文件
- [转]电子商务文件存储及读取服务的设计和开发思路
- 黑马程序员--OC自学笔记---02对象存储细节、NSString简介、多文件开发
- C#中通过读取配置文件动态创建泛型对象
- Java生成和解析XML格式文件和字符串的实例代码【dom4j中的SAXReader对象读取并解析xml文件】
- 文章生成器,Split方法截取字符串。从硬盘读取文件,和向硬盘存储文件参考代码
- 保存、方法-Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数-by小雨
- C#中通过读取配置文件动态创建泛型对象
- 关于存储—块,文件,对象
- 存储对象到文件
- 读取xml文件转成List<T>对象的两种方法
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]