ObjectInputStream缓存数据

DataManager

/**
 * 本地数据的存储
 * @author Administrator
 *
 */
public class DataManager {
    private static final String TEST_FILENAME="testorder.asc";
    private static final String TEST_ABCPATH="/data/data/deliver.jms.com.cn/files/testorder.asc";  //包名
    private static final String LOGIN="login";
    private static final String NOTIFYORDERS="notifyorders";
    private static final String PRODUCT="product";
    private static final String SKU="sku";
    private static final String ORDER="order";
    private static final String BITMAP="bitmap";
    private HashMap<String,ArrayList<Object>> DataList = null;
    private volatile static DataManager instance;
    
    private  DataManager(Context context){
        File file=new File(TEST_ABCPATH);
        if(!file.exists()){
            DataList = new HashMap<String,ArrayList<Object>>();
            DataList.put(SKU, new ArrayList<Object>());
            DataList.put(PRODUCT, new ArrayList<Object>());
            DataList.put(LOGIN, new ArrayList<Object>());
            DataList.put(NOTIFYORDERS, new ArrayList<Object>());
            DataList.put(ORDER, new ArrayList<Object>());
            DataList.put(BITMAP, new ArrayList<Object>());
            writeObjectToFile(context,DataList);
        }
    }
    /**
     * 得到本类的一个实例对象
     * @param context
     * @return
     */
    public static DataManager getInstance(Context context){
        if(instance == null){
            synchronized(DataManager.class){
                if(instance == null){
                    instance = new DataManager(context);
                }
            }
        }
        return instance;
    }
    /**
     * 将对象写入本地存储文件中
     */
    private synchronized void writeObjectToFile(Context context,HashMap<String, ArrayList<Object>> map){
        ObjectOutputStream oos=null;
        try {
            oos=new ObjectOutputStream(context.openFileOutput(TEST_FILENAME, Context.MODE_PRIVATE));
            //将对象写入文件中
            oos.writeObject(map);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            if(oos!=null){
                try {
                    oos.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            }
        }
        
        
    }
    /**
     * 读本地文件中的数据对象
     */
    private synchronized HashMap<String, ArrayList<Object>> readObjectFromFile(Context context,String fileName){
    
        HashMap<String, ArrayList<Object>> objects=null;
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(context.openFileInput(fileName));
            //得到本地数据的所有对象
            objects =  (HashMap<String, ArrayList<Object>>) (ois.readObject());
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(ois !=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return objects;
    }

    /**
     * 保存登陆数据
     */
    public  void saveLoginData(Context context,Object login){
        HashMap<String, ArrayList<Object>> map = readObjectFromFile(context,TEST_FILENAME);
        if(map!=null && login!=null){
            ArrayList<Object> loginData = map.get(LOGIN);
            if(loginData!=null){
                loginData.add(login);
                writeObjectToFile(context,map);
            }
        }
    }
    /**
     * 得到登陆数据
     */
    public ArrayList<Object> getLoginData(Context context){
        ArrayList<Object> loginData=null;
        HashMap<String, ArrayList<Object>> objects = readObjectFromFile(context,TEST_FILENAME);
        if(objects!=null){
             loginData = objects.get(LOGIN);
        }
        
        return loginData;
    }
}

 

posted @ 2015-01-13 17:54  每天学习一点点...  阅读(467)  评论(0编辑  收藏  举报