文件緩存方式 存入與取出

public class FileUtil {
    private static final String SAVE_PIC_PATH = Environment
            .getExternalStorageState().equalsIgnoreCase(
                    Environment.MEDIA_MOUNTED) ? Environment
            .getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";// 保存到SD卡
    public static String CACHEFILEPATH = SAVE_PIC_PATH + "/cache";

    public static int CACHE_TIME = 10000 * 6;//1分鐘
    public static int CACHE_TIME1 = 10000 * 6 * 60 * 2;//2個小時
    public static int CACHE_TIME2 = 10000 * 6 * 60 * 3;//3個小時
    public static int CACHE_TIME3 = 10000 * 6 * 60 * 5;//5個小時
    public static int CACHE_TIME4 = 10000 * 6 * 60 * 7;//7個小時
    public static int CACHE_TIME5 = 10000 * 6 * 60 * 12;//12個小時
    public static int CACHE_TIME6 = 10000 * 6 * 60 * 24;//24個小時



    public static String BOBYSD = "bobysd.txt";//通道頁 手動提現接口數據
    public static String BOBYZD = "bobyzd.txt";//通道頁 自動提現接口數據
    public static String BOBYFL = "bobyfeilv.txt";//費率顯示

    /**
     * 將數據寫入文件緩存
     *
     * @param ser
     * @param file
     * @return
     */

    public static boolean saveObject(String ser, File file, String name) {
        File newFile = new File(CACHEFILEPATH);
        if (!newFile.exists()) {
            newFile.mkdirs();
        }
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = new FileOutputStream(file, false);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            Log.i("緩存", name + " 寫入 緩存成功  ");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                fos.close();
                oos.close();

            } catch (Exception e) {
            }
        }
    }

    /**
     * 讀取對象
     *
     * @param file
     * @return
     */
    public static String readObject(File file, String name) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(file);   //獲得輸入流
            ois = new ObjectInputStream(fis);

            int length = fis.available();
            byte[] buffer = new byte[length];
            fis.read(buffer);

            String red = EncodingUtils.getString(buffer, "UTF-8");
            String s = red.substring(3, red.length());
            Log.i("緩存", name + ": 讀取緩存 s:" + s);
            return s;
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                ois.close();
            } catch (Exception e) {
            }
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
        return null;
    }

    /**
     * 判斷文件緩存是否過期
     *
     * @param data
     * @param time
     * @return
     */
    public static boolean isCacheDataFailure(File data, int time) {
        boolean failure = false;
        if ((System.currentTimeMillis() - data.lastModified()) > time)
            failure = true;
        else if (!data.exists())
            failure = true;
        return failure;
    }


    //刪除文件
    public static void delFile(String fileName) {
        File file = new File(FileUtil.CACHEFILEPATH + "/" + fileName);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                Log.i("緩存", "刪除文件 " + fileName + "成功!");
            } else {
                Log.i("緩存", "刪除文件 " + fileName + "失敗!");
            }
        }
        file.exists();
    }

    public static void clear(){
        FileUtil.delFile(FileUtil.BOBYSD);
        FileUtil.delFile(FileUtil.BOBYZD);
        FileUtil.delFile(FileUtil.BOBYFL);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章