在Android中缓存数据的最佳方法

时间:2010-01-05 03:41:40

标签: android

我有一个ArrayList个自定义的,简单的Serializable个对象我想缓存到磁盘并在重新启动时读取。我的数据很小,大约有25个对象,最多有5个列表,所以我认为SQLite会有点过分。在iPhone世界中,我会使用NSKeyedArchiverNSKeyedUnarchiver,效果很好。在Android上,我尝试使用FileOutputStreamObjectOutputStream进行此操作,虽然结果相同,但性能非常糟糕。是否有更好的(更快阅读)方法将小对象缓存到Android中的文件系统?

3 个答案:

答案 0 :(得分:19)

为了我的价值,我使用BufferedWriter / BufferedReader将我的一些String数据缓存到磁盘,而且速度非常快。事实上,它比将相同数据存储到SharedPreferences更快。代码就像这样(请注意,当您提供缓冲区大小时,事情发生得更快)

final BufferedWriter out = new BufferedWriter(new FileWriter(file), 1024);
out.write(stuff);
out.close();

答案 1 :(得分:4)

public class MyClass implements Serializable 
{
private static final long serialVersionUID = 1L;

public String title;
public String startTime;
public String endTime;
public String day;

public boolean classEnabled;


public MyClass(String title, String startTime, boolean enable) {
    this.title = title;
    this.startTime = startTime;
    this.classEnabled = enable;
}

public boolean saveObject(MyClass obj) {   
    final File suspend_f=new File(SerializationTest.cacheDir, "test");

    FileOutputStream   fos  = null;
    ObjectOutputStream oos  = null;
    boolean            keep = true;

    try {
        fos = new FileOutputStream(suspend_f);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (Exception e) {
        keep = false;
    } finally {
        try {
            if (oos != null)   oos.close();
            if (fos != null)   fos.close();
            if (keep == false) suspend_f.delete();
    } catch (Exception e) { /* do nothing */ }
    }

    return keep;
}

public MyClass getObject(Context c) {
    final File suspend_f=new File(SerializationTest.cacheDir, "test");

    MyClass simpleClass= null;
    FileInputStream fis = null;
    ObjectInputStream is = null;

    try {
        fis = new FileInputStream(suspend_f);
        is = new ObjectInputStream(fis);
        simpleClass = (MyClass) is.readObject();
    } catch(Exception e) {
        String val= e.getMessage();
    } finally {
        try {
            if (fis != null)   fis.close();
            if (is != null)   is.close();
        } catch (Exception e) { }
    }

    return simpleClass;  
}

并从活动中调用

 if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomObject");
else
cacheDir= getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();

MyClass m = new MyClass("umer", "asif", true);
boolean result = m.saveObject(m);

if(result)
Toast.makeText(this, "Saved object", Toast.LENGTH_LONG).show();

else
Toast.makeText(this, "Error saving object", Toast.LENGTH_LONG).show();   

 MyClass m = new MyClass();
 MyClass c = m.getObject(this);

 if(c!= null)

 Toast.makeText(this, "Retrieved object", Toast.LENGTH_LONG).show();

  else

 Toast.makeText(this, "Error retrieving object", Toast.LENGTH_LONG).show();

不要忘记在清单文件中使用write_external_storage权限。

答案 2 :(得分:3)

如果没有分析,很难知道,但我猜你的糟糕表现是使用ObjectOutputStream。您是否尝试过编写自己的writeObject(ObjectOutputStream) and readObject(ObjectOutputStream) methods,因为这可能会有助于提高效果。

您可以使用traceview工具查看应用程序运行缓慢的确切位置。看看at this question for instructions on how to use traceview