需要在Android中"保存缓存文件"的示例程序

And*_*era 7 android

我需要一个示例应用程序来演示如何在Android中保存缓存文件以及如何使用getCacheDir()方法?
任何人都可以帮我解决这个问题吗?我需要将文件保存在绝对目录中并需要解析该文件.
预先感谢.

Nat*_*ate 7

使用(在活动中):

String textToCache = "Some text";
boolean success = GetCacheDirExample.writeAllCachedText(this, "myCacheFile.txt", textToCache);
String readText = GetCacheDirExample.readAllCachedText(this, "myCacheFile.txt");
Run Code Online (Sandbox Code Playgroud)

GetCacheDirExample.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import android.content.Context;

public class GetCacheDirExample {

    public static String readAllCachedText(Context context, String filename) {
        File file = new File(context.getCacheDir(), filename);
        return readAllText(file);
    }

    public static String readAllResourceText(Context context, int resourceId) {
        InputStream inputStream = context.getResources().openRawResource(resourceId);
        return readAllText(inputStream);
    }

    public static String readAllFileText(String file) {
        try {
            FileInputStream inputStream = new FileInputStream(file);
            return readAllText(inputStream);
        } catch(Exception ex) {
            return null;
        }
    }

    public static String readAllText(File file) {
        try {
            FileInputStream inputStream = new FileInputStream(file);
            return readAllText(inputStream);
        } catch(Exception ex) {
            return null;
        }
    }

    public static String readAllText(InputStream inputStream) {
        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader buffreader = new BufferedReader(inputreader);

        String line;
        StringBuilder text = new StringBuilder();

        try {
            while (( line = buffreader.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        } catch (IOException e) {
            return null;
        }

        return text.toString();
    }

    public static boolean writeAllCachedText(Context context, String filename, String text) {
        File file = new File(context.getCacheDir(), filename);
        return writeAllText(file, text);
    }

    public static boolean writeAllFileText(String filename, String text) {
        try {
            FileOutputStream outputStream = new FileOutputStream(filename);
            return writeAllText(outputStream, text);
        } catch(Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public static boolean writeAllText(File file, String text) {
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            return writeAllText(outputStream, text);
        } catch(Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public static boolean writeAllText(OutputStream outputStream, String text) {
        OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream);
        BufferedWriter bufferedWriter = new BufferedWriter(outputWriter);
        boolean success = false;

        try {
            bufferedWriter.write(text);
            success = true;
        } catch(Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }

        return success;
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 很好,但写总是返回false.编辑writeAllText以获得true或false,如果是异常. (2认同)
  • 如果不存在,应将其放在文档中 (2认同)

Cha*_*uch 0

除非您确实需要将其缓存,否则您应该考虑更持久地存储文件:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

我还没有尝试使用缓存,但似乎一旦获得句柄,它就应该使用与持久文件相同的命令。