zoukankan      html  css  js  c++  java
  • File缓存

    /**
         * 保存对象
         * @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;
        }
  • 相关阅读:
    HTTP请求头的具体含意
    Python 之类与对象及继承
    PHP的一个牛逼的数组排序函数array_multisort
    mysqli返回受影响行数
    转:JS判断值是否是数字(两种方法)
    转:php中判断某个IP地址是否存在范围内
    php Closure::bind的用法(转)
    Drupal8入门文章推荐
    PHP通过api上传图片
    转:PHP中的使用curl发送请求(GET请求和POST请求)
  • 原文地址:https://www.cnblogs.com/pastor/p/5156676.html
Copyright © 2011-2022 走看看