android的缓存地址,android缓存与临时文件

应用程序程序在第一次打开的时候,我们会把一些常用的数据保存到本地;

或者应用程序在运行的时候,需要保存一些记录的(比如记事本),因为耗子的工作需要保存填写的一些表单在本地,所以就整理了一下如何简单的把数据保存到本地。

我们主要用到的方法就是下面这四个方法,看名字就可以看出来。

getExternalCacheDir()

getExternalFilesDir()

getCacheDir()

getFilesDir()

把他们分成了两组,一一来讲解

Context.getExternalFilesDir()可以获取到SDCard/Android/data/你的应用的包名/files/ 目录路径,一般长时间保存的数据

Context.getExternalCacheDir()可以获取到SDCard/Android/data/你的应用的包名/cache/ 目录路径,一般临时保存的数据

Context.getCacheDir()   该方法返回/data/data/你的应用的包名/cache目录路径

Context.getFilesDir()   该方法返回/data/data/你的应用的包名/file目录路径。

这两组的区别主要是一个是保存到内存卡、一个是手机内部。为什么我们要使用它们做路径呢?

如果使用上面的方法,当你的应用在被用户卸载后,SDCard/Android/

而且上面二个目录分别对应 设置->应用->应用详情里面的”清除数据“与”清除缓存“选项

如果要保存下载的内容,就不要放在以上目录下

下面放一个封装的小工具类。

public class DataCacheUtil {

/**

* 通过result生成缓存文件

*

* @param fileUrl

* @param resule

* @return

*/

public boolean create(String fileUrl, String resule) {

try {

String temp = resule;

BASE64Encoder base64en = new BASE64Encoder();

String tempjiami = base64en.encode(temp.getBytes("UTF-8"));

File file = new File(fileUrl);

if (!file.exists()) {

file.createNewFile();

}

FileWriter fw = new FileWriter(fileUrl);

fw.write(tempjiami, 0, tempjiami.length());

fw.flush();

fw.close();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

* 删除缓存文件

* @param fileURL

*/

public void deleteXML(String fileURL){

File file = new File(fileURL);

if (file.exists()) {

file.delete();

}

}

/**

* 读取文件中的内容按照字符读取

* @param fileURL

*/

public String readFileByChars(String fileURL) {

StringBuffer result = new StringBuffer();

StringBuffer tempResult = new StringBuffer();

Reader reader = null;

try {

char[] tempchars = new char[10];

int charread = 0;

reader = new InputStreamReader(new FileInputStream(fileURL));

// 读入多个字符到字符数组中,charread为一次读取字符数

while ((charread = reader.read(tempchars)) != -1) {

// 屏蔽掉\r不显示

if ((charread == tempchars.length)

&& (tempchars[tempchars.length - 1] != '\r')) {

tempResult.append(tempchars);

} else {

for (int i = 0; i < charread; i++) {

if (tempchars[i] == '\r') {

continue;

} else {

tempResult.append(tempchars[i]);

}

}

}

}

BASE64Decoder base64De = new BASE64Decoder();

result.append(new String(base64De.decodeBuffer(tempResult.toString()), "UTF-8" ));

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

return result.toString();

}

/**

* 获取可用的缓存目录

* @param context

* @param filename

* @return

*/

public static String getCacheDir(Context context,String filename){

String fileURL = null;

//如果外部存储为空时则用内部存储的路径

if (context.getExternalCacheDir() != null) {

fileURL = context.getExternalCacheDir().toString()+"/"+ filename;

} else {

fileURL = context.getCacheDir().toString() + "/"+ filename;

}

return fileURL;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值