簡體   English   中英

如何刪除緩存中存儲的文件? (機器人)

[英]How do I delete a file stored in cache? (android)

我無法刪除存儲在緩存中的文件。 我將緩存用於多種用途。 我正在讀寫,但無法刪除。 有人可以幫我嗎?

//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();
   }

context.openFileOutput(key將文件寫入內部存儲器。您可以使用getFilesDir()找到的路徑,看起來像/data/data/<yourpackagename>/files

因此,如果要刪除文件“鍵”,則必須將File file = new File(path)String path = getFilesDir().getAbsolutePath() + "/" + key;

並使用file.exists()檢查文件是否存在!

使用它來清除應用程序數據。

  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 &amp;&amp; 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();
    }

與緩存目錄一樣,您的應用程序還具有一個特定於應用程序的目錄,用於保存文件。 該目錄中的文件將一直存在,直到應用程序將其明確刪除或將其卸載為止。 通常,您可以使用Context.getFilesDir()訪問此目錄。 這可以顯示為應用程序信息屏幕上的各種內容,但是在屏幕快照中,這是“ USB存儲數據”。

注意:如果要顯式放置在外部媒體(通常是SD卡)上,則可以使用Context.getExternalFilesDir(String type)

簡單的緩存管理器:

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;
    }
}

注意:緩存的目的是減少網絡活動,減少冗長的過程並在您的應用程序中提供響應式UI。

參考: 何時在Android中清除緩存目錄?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM