Java 如何删除缓存中存储的文件?(安卓)
我无法删除存储在缓存中的文件。我将缓存用于多种用途。我正在阅读和写作,但无法删除。有人能帮我吗Java 如何删除缓存中存储的文件?(安卓),java,android,fileinputstream,Java,Android,Fileinputstream,我无法删除存储在缓存中的文件。我将缓存用于多种用途。我正在阅读和写作,但无法删除。有人能帮我吗 //write public static void writeObject(Context context, String key, Object object) throws IOException { Log.d("Cache", "WRITE: context"); Fil
//write
public static void writeObject(Context context, String key, Object object)
throws IOException {
Log.d("Cache", "WRITE: context");
FileOutputStream fos = context.openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
}
//read
public static Object readObject(Context context, String key) throws IOException,
ClassNotFoundException {
FileInputStream fis = context.openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
return object;
}
//delete
public static void clearCahe(String key) throws IOException,ClassNotFoundException {
File file = new File(key);
file.delete();
}
使用此选项清除应用程序数据
public void clearApplicationData()
{
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
public void clearApplicationData()
{
文件缓存=getCacheDir();
File appDir=新文件(cache.getParent());
if(appDir.exists()){
String[]children=appDir.list();
for(字符串s:子项){
如果(!s.equals(“lib”)){
deleteDir(新文件(appDir,s));Log.i(“TAG”,“*************************文件/data/data/APP_包/“+s+”已删除****************************”;
}
}
}
}
公共静态布尔deleteDir(文件目录)
{
if(dir!=null&;dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
文件
与缓存目录一样,您的应用程序也有一个特定于应用程序的目录,用于保存文件。此目录中的文件将一直存在,直到应用程序明确删除它们或卸载应用程序。通常使用Context.getFilesDir()
访问此目录。这可以在应用程序信息屏幕上显示为各种内容,但在您的屏幕截图中,这是“USB存储数据”
注意:如果要显式放置在外部媒体(通常是SD卡)上,可以使用Context.getExternalFilesDir(字符串类型)
简单缓存管理器:
public class CacheManager {
private static final long MAX_SIZE = 5242880L; // 5MB
private CacheManager() {
}
public static void cacheData(Context context, byte[] data, String name) throws IOException {
File cacheDir = context.getCacheDir();
long size = getDirSize(cacheDir);
long newSize = data.length + size;
if (newSize > MAX_SIZE) {
cleanDir(cacheDir, newSize - MAX_SIZE);
}
File file = new File(cacheDir, name);
FileOutputStream os = new FileOutputStream(file);
try {
os.write(data);
}
finally {
os.flush();
os.close();
}
}
public static byte[] retrieveData(Context context, String name) throws IOException {
File cacheDir = context.getCacheDir();
File file = new File(cacheDir, name);
if (!file.exists()) {
// Data doesn't exist
return null;
}
byte[] data = new byte[(int) file.length()];
FileInputStream is = new FileInputStream(file);
try {
is.read(data);
}
finally {
is.close();
}
return data;
}
private static void cleanDir(File dir, long bytes) {
long bytesDeleted = 0;
File[] files = dir.listFiles();
for (File file : files) {
bytesDeleted += file.length();
file.delete();
if (bytesDeleted >= bytes) {
break;
}
}
}
private static long getDirSize(File dir) {
long size = 0;
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
size += file.length();
}
}
return size;
}
}
注意:缓存的目的是减少网络活动,
长流程,并在应用程序中提供响应性用户界面
参考:.
上下文。openFileOutput(键
将文件写入内部内存。您可以使用GetFileDir()找到的路径类似于/data/data//files
因此,如果要删除文件“key”,必须将file file file=new file(path)
的路径设置为String path=getFilesDir().getAbsolutePath()+“/”+key;
并使用file.exists()检查文件是否存在!请记录delete方法的返回类型。请打印堆栈跟踪,可能是特定文件不可用。可以这样定义;file file=new file(getFilesDir(),fileName);