I have searched everywhere, and I tried different solutions, but the result is blank/empty/{}. Is it possible to save LinkedHashMap, so that it can be retrieved later?
//Save HashMap to Internal Storage
public void SaveHashMapToInternalStorage(String SavedData, LinkedHashMap<Integer, Integer> linkedHashMapList)
throws FileNotFoundException, IOException {
try{
File fileOne = new File(SavedData);
FileOutputStream fileOutputStream = new FileOutputStream(fileOne);
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
oos.writeObject(linkedHashMapList);
oos.flush();
oos.close();
fileOutputStream.close();
}catch(Exception e){}
}
//Load HashMap from Internal Storage
public LinkedHashMap<Integer, Integer> LoadHashMapFromInternalStorage(String SavedData) throws IOException {
LinkedHashMap<Integer, Integer> linkedHashMapList = new LinkedHashMap<Integer, Integer>();
try{
File toRead = new File(SavedData);
FileInputStream fileInputStream = new FileInputStream(toRead);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
linkedHashMapList = (LinkedHashMap<Integer, Integer>)objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
for(Entry<Integer, Integer> m :linkedHashMapList.entrySet()){
linkedHashMapList.put(m.getKey(), m.getValue());
}
}catch(Exception e){}
return linkedHashMapList;
}
Can someone please help me out?
Thanks alot
Update:
I placed the Toast inside the catch(Exception e), sure it is. There is something wrong in the method.
I tried to load this in Oncreate method:
FirstRunMethod = SharedPrefs.getBoolean("FIRST_RUN", false);
if (!FirstRunMethod) {
// do the thing for the first time
prefs_editor.putBoolean("FIRST_RUN", true);
prefs_editor.apply();
.......
.......
try {
SaveHashMapToInternalStorage("file.txt", PicResolution);
} catch (FileNotFoundException e) { );
} catch (IOException e) { }
}
PictureResWidthHashMap = LoadHashMapFromInternalStorage("file.txt");
....
.....
public LinkedHashMap<Integer, Integer> LoadHashMapFromInternalStorage(String SavedData) {
LinkedHashMap<Integer, Integer> linkedHashMapLIST = new LinkedHashMap<Integer, Integer>();
try{
File toRead = new File(SavedData);
FileInputStream fileInputStream = new FileInputStream(toRead);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
linkedHashMapLIST = (LinkedHashMap<Integer, Integer>)objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
for(Entry<Integer, Integer> m :linkedHashMapLIST.entrySet()){
linkedHashMapLIST.put(m.getKey(), m.getValue());
}
}catch(Exception e){Toast.makeText(getBaseContext(), "CANT LOAD ERROR", Toast.LENGTH_LONG).show(); }
return linkedHashMapLIST;
}
Why it worked for the other person, but it is not working for me? This makes no sense to me at all.
I think this will help you solve your problem:
Try this:
//Save HashMap to Internal Storage
public void SaveHashMapToInternalStorage(String SavedData, LinkedHashMap<Integer, Integer> linkedHashMapList) {
try{
FileOutputStream fos = this.openFileOutput(SavedData, Context.MODE_PRIVATE);
ObjectOutputStream s = new ObjectOutputStream(fos);
s.writeObject(linkedHashMapList);
s.close();
}catch(Exception e){ }
}
//Load HashMap from Internal Storage @SuppressWarnings("unchecked")
public LinkedHashMap<Integer, Integer> LoadHashMapFromInternalStorage(String SavedData) {
LinkedHashMap<Integer, Integer> linkedHashMapLIST = new LinkedHashMap<Integer, Integer>();
try{
FileInputStream fileInputStream = this.openFileInput(SavedData);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
linkedHashMapLIST = (LinkedHashMap<Integer, Integer>) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
}catch(Exception e){ }
return linkedHashMapLIST;
}
Probably you made some mistakes in initialization. I have run this code and got the desired output.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
public class LinkedHashMapExample {
public LinkedHashMapExample() {
// TODO Auto-generated constructor stub
}
public void SaveHashMapToInternalStorage(String SavedData, LinkedHashMap<Integer, Integer> linkedHashMapList)
throws FileNotFoundException, IOException {
try{
File fileOne = new File(SavedData);
FileOutputStream fileOutputStream = new FileOutputStream(fileOne);
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
oos.writeObject(linkedHashMapList);
oos.flush();
oos.close();
fileOutputStream.close();
}catch(Exception e){}
}
//Load HashMap from Internal Storage
public LinkedHashMap<Integer, Integer> LoadHashMapFromInternalStorage(String SavedData) throws IOException {
LinkedHashMap<Integer, Integer> linkedHashMapList = new LinkedHashMap<Integer, Integer>();
try{
File toRead = new File(SavedData);
FileInputStream fileInputStream = new FileInputStream(toRead);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
linkedHashMapList = (LinkedHashMap<Integer, Integer>)objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
for(Entry<Integer, Integer> m :linkedHashMapList.entrySet()){
linkedHashMapList.put(m.getKey(), m.getValue());
}
}catch(Exception e){}
return linkedHashMapList;
}
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException, IOException
{
// TODO Auto-generated method stub
LinkedHashMap<Integer, Integer> linkedHashMap = new LinkedHashMap<Integer,Integer>();
linkedHashMap.put(0, 0);
linkedHashMap.put(1, 1);
linkedHashMap.put(2, 4);
LinkedHashMapExample linkedHashMapExample = new LinkedHashMapExample();
linkedHashMapExample.SaveHashMapToInternalStorage("file.txt", linkedHashMap);
LinkedHashMap<Integer, Integer> linkedHashMapOutput = linkedHashMapExample.LoadHashMapFromInternalStorage("file.txt");
for(int i=0; i <linkedHashMapOutput.size(); i++)
{
System.out.println(linkedHashMapOutput.get(i));
}
}
}
In output I got:
0 1 4
Hope you can find out your problem from this example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With