How can I store a data structure like Hashmap inside Android?

In my Android application, I am trying to preserve the map structure, for example: Map<String, Map<String, String>> using internal storage. I studied the use of SharedPreferences , but as you know, this only works when saving primitive data types. I tried to use FileOutputStream , but that only allows me to write in bytes ... Do I need to serialize the Hashmap somehow and then write to a file?

I tried reading through http://developer.android.com/guide/topics/data/data-storage.html#filesInternal , but I can not find my solution.

Here is an example of what I'm trying to do:

 private void storeEventParametersInternal(Context context, String eventId, Map<String, String> eventDetails){ Map<String,Map<String,String>> eventStorage = new HashMap<String,Map<String,String>>(); Map<String, String> eventData = new HashMap<String, String>(); String REQUEST_ID_KEY = randomString(16); . //eventData.put... . //eventData.put... eventStorage.put(REQUEST_ID_KEY, eventData); FileOutputStream fos = context.openFileOutput(EVENT_FILENAME, Context.MODE_PRIVATE); fos.write(eventStorage) //This is wrong but I need to write to file for later access.. } 

What is the best way to store this type of data structure inside an Android application? Sorry if this seems like a dumb question, I'm very new to Android. Thanks in advance.

+6
source share
3 answers

HashMap is serializable, so you can just use FileInputStream and FileOutputStream in combination with ObjectInputStream and ObjectOutputStream .

To write a HashMap to a file:

 FileOutputStream fileOutputStream = new FileOutputStream("myMap.whateverExtension"); ObjectOutputStream objectOutputStream= new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(myHashMap); objectOutputStream.close(); 

To read a HashMap from a file:

 FileInputStream fileInputStream = new FileInputStream("myMap.whateverExtension"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Map myNewlyReadInMap = (HashMap) objectInputStream.readObject(); objectInputStream.close(); 
+15
source

Record:

 FileOutputStream fos = context.openFileOutput(EVENT_FILENAME, Context.MODE_PRIVATE); ObjectOutputStream s = new ObjectOutputStream(fos); s.writeObject(eventStorage); s.close(); 

Reading is done in reverse order and discarding your type in readObject

0
source

+1 for Steve P's answer, but it doesn't work directly, and while reading, I get a FileNotFoundException, I tried this, and it works fine.

To record

 try { FileOutputStream fos = context.openFileOutput("YourInfomration.ser", Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(myHashMap); oos.close(); } catch (IOException e) { e.printStackTrace(); } 

And read

 try { FileInputStream fileInputStream = new FileInputStream(context.getFilesDir()+"/FenceInformation.ser"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Map myHashMap = (Map)objectInputStream.readObject(); } catch(ClassNotFoundException | IOException | ClassCastException e) { e.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/949764/


All Articles