/**
     * 保存对象
     * @param ser
     * @param file
     * @throws IOException
     */
    public boolean saveObjectList(ArrayList<NewsType> ser, String file) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try{
            fos = openFileOutput(file, MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            oos.flush();
            oos.close();
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }finally{
            try {
                oos.close();
            } catch (Exception e) {}
            try {
                fos.close();
            } catch (Exception e) {}
        }
    }
 
 
    /**
     * 读取对象
     * @param file
     * @return
     * @throws IOException
     */
    public ArrayList<NewsType> readObjectList(String file){
        if(!isExistDataCache(file))
            return null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try{
            fis = openFileInput(file);
            ois = new ObjectInputStream(fis);
            return (ArrayList<NewsType>)ois.readObject();
        }catch(FileNotFoundException e){
        }catch(Exception e){
            e.printStackTrace();
            //反序列化失败 - 删除缓存文件
            if(e instanceof InvalidClassException){
                File data = getFileStreamPath(file);
                data.delete();
            }
        }finally{
            try {
                ois.close();
            } catch (Exception e) {}
            try {
                fis.close();
            } catch (Exception e) {}
        }
        return null;
    }

File缓存的更多相关文章

  1. Laravel之路——file缓存修改为redis缓存

    1.Session: 修改.evn文件: SESSION_DRIVER:redis (如果还不行的话,修改config/session.php的driver) 2.缓存修改为redis 注意:使用 L ...

  2. php 的file 缓存

    PDO方式连接数据库类 <?php /** * @author 黄功延 * createTime 2018/5/28 0028 09:44 */ class Db { //私有化数据库连接数据, ...

  3. django 缓存、中间件、信号、CSRF 详解

    中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项 ...

  4. php使用文件缓存

    使用php读取mysql中的数据很简单,数据量不大的时候,mysql的性能还是不错的.但是有些查询可能比较耗时,这时可以把查询出的结果,缓存起来,减轻mysql的查询压力. 缓存的方法有几种:使用me ...

  5. ThinkPHP的缓存技术

    原文:ThinkPHP的缓存技术 如果没有缓存的网站是百万级或者千万级的访问量,会给数据库或者服务器造成很大的压力,通过缓存,大幅减少服务器和数据库的负荷.假如我们 把读取数据的过程分为三个层,第一个 ...

  6. Discuz的缓存体系

    参考文档:<http://dev.discuz.org/wiki/index.php?title=缓存机制> Discuz中涉及数据缓存的地方有: 1. session Dz的sessio ...

  7. php 文件缓存

    http://www.oschina.net/code/snippet_162279_6098 <?php class cache {       private static $_instan ...

  8. php文件缓存

    1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...

  9. php 缓存 加速缓存

    PHP四大加速缓存器opcache,apc,xcache,eAccelerator eAccelerator,xcache,opcache,apc(偏数据库缓存,分系统和用户缓存)是PHP缓存扩展, ...

随机推荐

  1. 将Cocos2dX渲染到MFC窗口上

    引用:http://www.cnblogs.com/windeer/archive/2012/11/18/2767750.html 引言 现在智能手机已经慢慢进入大众化,移动类应用开始火爆起来,游戏类 ...

  2. word转html方法

    在网上看了一篇关于word转html的文章,感觉不错,和大家分享一下. /// <summary> /// word转成html /// </summary> /// < ...

  3. Azure sdk for python

    http://www.oschina.net/translate/python-windows-azure Len  6:17:54 PM __author__ = 'len.li' from azu ...

  4. Elementory os install .net core

    ref link : https://www.microsoft.com/net/core#linuxubuntu sudo sh -c 'echo "deb [arch=amd64] ht ...

  5. c#委托、事件、Observer

    委托和事件在.NET Framework[1] 中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易. 中文名 委托 外文名 Delegate 编程语言 C# 作     ...

  6. Android APK反编译easy 详解

    在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能会很想知道这些效果界面是怎么去实现的,这时,你便可以对改应用 ...

  7. 修改 MyEclipse 中的 jsp 和 servlet 模板

    找到  MyEclipse/Common/plugins/com.genuitec.eclipse.wizards_9.0.0.me201211011550.jar 这个文件(wizards后面的数字 ...

  8. 如何利用php array_multisort函数 对数据库排序

    数据库中有4个字段分别是id,volume,edition,name. 要求对查询结果按照volume+edition从大到小排序.下面将一下array_multisort函数array_multis ...

  9. Python—I/O多路复用

    一.I/O多路复用概念: 监听多个描述符的状态,如果描述符状态改变,则会被内核修改标志位,从而被进程获取进而进行读写操作 二.select,poll,epoll select模块,提供了:select ...

  10. myisam压缩(前缀压缩)索引

    myisam使用前缀压缩来减少索引的大小,从而让更多的索引可以放入内存中,默认只压缩字符串,但通过参数配置也可以对整数做压缩,myisam压缩每个索引块的方法是,先完全保存索引块中的第一个值,然后将其 ...