Android Question save user defined type object in ascii format (csv or other)

giggetto71

Active Member
Licensed User
Longtime User
Hi,
I have a user defined type object. something like:

B4X:
Type Test1(Field1 as string, Field2 as string,Field3 as string)
dim Obj1 as Test1
Obj1.initialize
Obj1.Field1 = "text1"
Obj1.Field2 = "text2"
Obj1.Field3 = "text3"

I would like to give the user the ability to save in a text format (csv or anything else). In the past I have created manually a csv string and then used the WriteString method of File object but in this case my real Obj1 is pretty complex (many fields) so I was hoping for something simple.
Of course I also would like to be able to read the generated file to fill the Obj1.
Thanks!
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    ' Definition
    Dim Obj1 As Test1
    Obj1.initialize
    Obj1.Field1 = "text1"
    Obj1.Field2 = "text2"
    Obj1.Field3 = "text3"
    
    ' Write to disc
    Dim ser As B4XSerializator ' RANDOMCCESSFILE
    Dim typebytes() As Byte = ser.ConvertObjectToBytes(Obj1)
    File.WriteBytes(File.DirInternal,"test.obj",typebytes)
    
    ' Read from Disc
    Dim bytesread() As Byte = File.ReadBytes(File.DirInternal,"test.obj")
    Dim Obj1Read As Test1 = ser.ConvertBytesToObject(bytesread)
    Log($"Test1.Field1 = ${Obj1Read.Field1}"$)
    Log($"Test1.Field2 = ${Obj1Read.Field2}"$)
    Log($"Test1.Field3 = ${Obj1Read.Field3}"$)
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
only one question: I am struggling to locate the "test.obj" file on the phone..It should be in File.DirInternal but when I look for it connecting the phone to the PC I just don't find it..can you help?
thanks!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
only one question: I am struggling to locate the "test.obj" file on the phone..It should be in File.DirInternal but when I look for it connecting the phone to the PC I just don't find it..can you help?
Only your app can access this folder.

You can use RP.GetSafeDirDefaultExternal instead. Log it to see the full path.
 
Upvote 0
Top