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);
}
}
文件缓存方式 存入与取出
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.