Failure reading a map from a file

GuyBooth

Active Member
Licensed User
Longtime User
I've been struggling for a couple of days with trying to save and retrieve a map file. The map contains a few string values and a list. The problem is that if the list is being saved, and I don't know if that is true, I can't retrieve it. Here's a piece of test code:

B4X:
Sub Test_Click

'' Test for saving and retrieving a map
Dim lst1, lst2 As List
Dim map1 As Map

lst1.Initialize
lst1.Add("Number1")
lst1.Add("Number2")
lst1.Add("Number3")

map1.Initialize
map1.Put("Text1","Text 1")
map1.Put("Text2","Text 2")
map1.Put("List",lst1)
map1.Put("Text3","Text 3")

lst2 = map1.Get("List")
Log(map1.Get("Text1"))
Log(map1.Get("Text2"))
Log(map1.Get("Text3"))
For i = 0 To lst2.Size - 1
   Log(lst2.Get(i))
Next
' Now save the map to a file
File.WriteMap("/mnt/sdcard/mme/data", "TestFile", map1)
' Empty the lists and map
map1.Clear
lst1.Clear
lst2.Clear
' And read the map back
map1 = File.ReadMap("/mnt/sdcard/mme/data", "TestFile")
' And read the results
Log(map1.Get("Text1"))
Log(map1.Get("Text2"))
Log(map1.Get("Text3"))

' We're good so far - but the program halts on this line (with no info from the debugger):
lst2 = map1.Get("List")
For i = 0 To lst2.Size - 1
   Log("Read back" & lst2.Get(i))
Next

End Sub
I've tried a number of variations with no success.
Can anyone help me on this?
 

stevel05

Expert
Licensed User
Longtime User
If you look in the log you should see a ClassCastException, from the documentation:

WriteMap (Dir As String, FileName As String, Map As Map)

Creates a new file and writes the given map. Each key value pair is written as a single line.
All values are converted to strings.
See this link for more information about the actual format: Properties format.
You can use File.ReadMap to read this file.

As stated, all values are converted to strings which will not then be reconverted to a list by ReadMap causing the ClassCastException as you are trying to assign it back to a list. I'm sure there's a better explanation somewhere on the forum if you need one but you could either use File.writelist separately for the list, or probably better look at the KeyValueStore class for this kind of task.
 
Last edited:
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
If you look in the log you should see a ClassCastException, from the documentation:
I get no exception messages in the log, and absolutely nothing in the debugger, which on my system has been sketchy at best for some time now.

As stated, all values are converted to strings
You are absolutely correct, and I missed this in the documentation
which will not then be reconverted to a list by ReadMap
the documentation doesn't quite say this - in fact it implies that I should be able to extrract the information albeit in string form, although it doesn't say how.
To be honest I think the documentation is less than clear on this, and some comment on these file limitations would not be amiss in the section that talks about using maps (e.g. "note that if you are likely to want to save a map containing objects other than strings (or primitives) to a file you should consider a different approach." Would have saved me a chunk of time anyway :)

I appreciate your response - I will look at the KeyValueStore approach, although it seems to me like a sledgehammer for something that should have been very straightforward.
 
Upvote 0
Top