不读取或写入内部存储android
我正在尝试将地图存储到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是
Should be