### Save Order of Map

This example shows how to write and read a map keeping the map order.
Required is the jRandomAccessFile Library.
The layout file main.bjl contains a single view taMapContent as TextArea.
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private taMapContent As TextArea
Private mapTest As Map
Private mapTestFile As String = "test.map"
Dim raf As RandomAccessFile
End Sub`
Region App Setup
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main.bjl")
MainForm.SetFormStyle("UTILITY")
MainForm.Title = "B4J Howto Map To / From File"
MainForm.Show
AppInit
End Sub`
'Init controls, settings etc.
`Sub AppInit
Dim sb As StringBuilder
sb.Initialize
mapTest.Initialize
If File.Exists(File.DirApp, mapTestFile) = False Then
MapCreateNew
raf.Initialize(File.DirApp, mapTestFile, False)
MapWriteToFile
Else
raf.Initialize(File.DirApp, mapTestFile, False)
MapReadFromFile
End If
For i = 0 To mapTest.Size - 1
sb.Append(mapTest.GetKeyAt(i) & " = " & mapTest.GetValueAt(i)).Append(CRLF)
Next
taMapContent.Text = sb.ToString
End Sub`
`Sub AppClose
MapWriteToFile
MainForm.Close
End Sub`
`Sub MainForm_CloseRequest (EventData As Event)
AppClose
End Sub`
`Sub MapCreateNew
mapTest = CreateMap("MapItem1":1, "MapItem2":2, "MapItem3":3)
End Sub`
`Sub MapReadFromFile
mapTest = raf.ReadB4XObject(0)
End Sub`
`Sub MapWriteToFile
raf.WriteB4XObject(mapTest, 0)
'raf.Close
End Sub`

Changed: , Category: Map, File: T6C217M1602.htm, ID: 1602