android内部的存储结构和路径获取方法:
http://www.tuicool.com/articles/AvUnqiy
清除本地数据和缓存:
http://www.cnblogs.com/rayray/p/3413673.html
计算缓存和内存数据的大小:
http://blog.csdn.net/wf_zeng/article/details/9378065
计算缓存大小以及删除的方法:
http://www.2cto.com/kf/201503/385492.html
这文章里面的文件删除方法有一些问题:没有循环是文件夹的删除方法;另外:
File.list(); 获取的是该文件夹下的所有子文件的相对路径;
File.listFiles();获取的是该文件夹下的所有子文件的绝对路径;
android的文件保存工具类:
import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import org.xmlpull.v1.XmlSerializer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
public class FileService {
/**
* save data to phone rom
* @param context 上下文
* @param fileName 保存的文件名
* @param name 用户名
* @param password 密码
* @return
*/
public static boolean saveToRom(Context context, String fileName, String name, String password){
// File file = new File("/data/data/cn.itcast.login/a.txt");
//相当于存储到/data/dat/packageName/目录下
File file = new File(context.getExternalFilesDir("test"), fileName);
// 如果没有指定访问的模式 ,文件的模式 默认是私有的权限.
// 只有当前的应用程序可以读写这个文件 ,别的应用程序是不可以操作这个文件.
try {
FileOutputStream fos=new FileOutputStream(file);
fos.write((name+":"+password).getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 保存数据到手机的rom空间的缓存目录
* 作用 保存应用程序的临时数据, 在手机内存不足的时候 系统会释放掉这块空间
* 用户也可以手工的释放掉这块空间
* @param context 上下文
* @param name 用户名
* @param password 密码
* @return
*/
public static boolean saveToRomCache(Context context,String fileName,String name,String password){
File file=new File(context.getCacheDir(),fileName);///data/dat/packageName/
try {
FileOutputStream fos=new FileOutputStream(file);
fos.write((name+":"+password).getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* sava data to externalStorage【外部存储卡】
* @param context
* @param fileName
* @param name
* @param password
* @return
*/
public static boolean saveToSD(Context context,String fileName,String name,String password){
//相当于存储到/mnt/sdcard/目录下
//在保存数据到sd卡之前 ,最好判断一下 用户是否有sd卡 sd是否可用.
File file=new File(Environment.getExternalStorageDirectory(),fileName);
try {
FileOutputStream fos=new FileOutputStream(file);
fos.write((name+":"+password).getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将用户输入的数据以xml文件格式保存到手机rom空间
* @param context
* @param name
* @param password
* @return
*/
public static boolean saveToXML(Context context,String name,String password){
File file=new File(context.getFilesDir(),"info.xml");
try {
FileOutputStream fos=new FileOutputStream(file);
XmlSerializer serial= Xml.newSerializer();
//初始化一下xml的序列化器
serial.setOutput(fos, "UTF-8");
serial.startDocument("UTF-8", true);
serial.startTag(null, "map");
serial.startTag(null, "name");
serial.text(name);
serial.endTag(null, "name");
serial.startTag(null, "password");
serial.text(password);
serial.endTag(null, "password");
serial.endTag(null, "map");
serial.endDocument();
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 从rom文件中读取存储的内容
* @param context
* @param fileName
* @return
*/
public static Map<String,String> readFromRom(Context context, String fileName){
File file=new File(context.getExternalFilesDir("test"),fileName);
try {
FileInputStream fis=new FileInputStream(file);
byte[] result=StreamTools.getBytes(fis);
String[] data=new String(result).split(":");
if (data!=null&&data.length>=2){
String name=data[0];
String password=data[1];
Map<String,String> map=new HashMap<String, String>();
map.put("name", name);
map.put("password", password);
return map;
}
return new HashMap<String, String>();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}