不读取或写入内部存储android

发布于 2024-11-23 23:49:04 字数 1515 浏览 2 评论 0原文

我正在尝试将地图存储到 android 的内部存储中。

我的代码:

    private void saveFavorite(){
    LinkedHashMap<String, LinkedList<MyCustomObject>> favorites = new LinkedHashMap<String, LinkedList<MyCustomObject>>();

    try{
        InputStream file = openFileInput(PATH);
        BufferedInputStream buffer = new BufferedInputStream( file );
        ObjectInput input = new ObjectInputStream ( buffer );
        try{
            Object o = input.readObject();
            if(o instanceof LinkedHashMap<?, ?>)
                favorites = (LinkedHashMap<String, LinkedList<MyCustomObject>>)o;
        }
        finally{
            input.close();
        }
    }
    catch(ClassNotFoundException ex){
    }
    catch(IOException ex){
    }

    String favoriteName = "asd";


    favorites.put(favoriteName, myobject);


    FileOutputStream fos;
    try {
        fos = openFileOutput(PATH, MODE_APPEND);
        BufferedOutputStream buffer = new BufferedOutputStream( fos );
        ObjectOutput output = new ObjectOutputStream( buffer );
        try{
            output.writeObject(favorites);
        }finally{
            output.close();
        }
    }catch(IOException ex){
    }
}

MyCustomObject 实现 Serialized

在调试时我没有看到任何问题。它似乎读取了一个空地图,然后在地图上写入了一个值,但是当我再次读取它时,地图是空的。

请帮忙。

更新: 我在 /data/data/my_project_package_struct/files/ 里面找到了 一个类似于我的 var PATH 的文件。每次我调用保存方法时它的大小都会增加,所以我认为它写得很好,但我不知道我做错了什么。

I'm trying to store a Map into android's internal storage.

My code:

    private void saveFavorite(){
    LinkedHashMap<String, LinkedList<MyCustomObject>> favorites = new LinkedHashMap<String, LinkedList<MyCustomObject>>();

    try{
        InputStream file = openFileInput(PATH);
        BufferedInputStream buffer = new BufferedInputStream( file );
        ObjectInput input = new ObjectInputStream ( buffer );
        try{
            Object o = input.readObject();
            if(o instanceof LinkedHashMap<?, ?>)
                favorites = (LinkedHashMap<String, LinkedList<MyCustomObject>>)o;
        }
        finally{
            input.close();
        }
    }
    catch(ClassNotFoundException ex){
    }
    catch(IOException ex){
    }

    String favoriteName = "asd";


    favorites.put(favoriteName, myobject);


    FileOutputStream fos;
    try {
        fos = openFileOutput(PATH, MODE_APPEND);
        BufferedOutputStream buffer = new BufferedOutputStream( fos );
        ObjectOutput output = new ObjectOutputStream( buffer );
        try{
            output.writeObject(favorites);
        }finally{
            output.close();
        }
    }catch(IOException ex){
    }
}

MyCustomObject implements Serializable

While debugging I don't see any problem. It seems it reads an empty map, then writes the map with a value but when I read it again, map is empty.

Help please.

UPDATE:
I have found inside /data/data/my_project_package_structure/files/
a file called like my var PATH. It's growing in size each time I call my save method so I think it writes well but I don't know what I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2024-11-30 23:49:04
try {
    **fos = openFileOutput(PATH, MODE_APPEND);**
    BufferedOutputStream buffer = new BufferedOutputStream( fos );

应该是

    **fos = openFileOutput(PATH, MODE_PRIVATE);**
try {
    **fos = openFileOutput(PATH, MODE_APPEND);**
    BufferedOutputStream buffer = new BufferedOutputStream( fos );

Should be

    **fos = openFileOutput(PATH, MODE_PRIVATE);**
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文