Android Question WriteList adds a carriage return at the end of the list.....

andyp

Member
Licensed User
Hi. I am using WriteList to save a file - works great - except it adds a carriage return to the end of the list. So a list with 2 items is actually 3 lines long. The (non-android) software I use to open the created file checks the number of lines - and rejects my file as it is one line too many. Any idea how to stop (or remove) this extra carriage return? Thank you.....

B4X:
        Dim saved As List
        saved.Initialize
        saved.Add("Item1")
        saved.Add("Item2")
        File.MakeDir(File.DirInternal, "/mydirectory")
        File.WriteList(File.DirInternal&"/mydirectory", "savedfile", saved)
 

DonManfred

Expert
Licensed User
Longtime User
Method_636.png
File. WriteList (Dir As String, FileName As String, List As List)

Writes each item in the list as a single line.
Note that a value containing CRLF will be saved as two lines (which will return two item when read with ReadList).
All values will be converted to strings.
 
Upvote 0

andyp

Member
Licensed User
Yes, I saw that thank you :) I guess I am asking is there any work around, or method to post-process the file to remove the final carriage return?
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
you are right i tested 4 Variants of writing files (B4j)
in all variants the result are in my case 4 Lines by 3 Items

B4X:
' Variant 1
Dim li As List
li.Initialize2( Array As Int( 1,2,3))
File.WriteList( File.DirApp, "list.txt", li)

' Variant 2
Dim sb As StringBuilder
sb.Initialize
For Each item As Int In li
    sb.Append( item).Append( CRLF)
Next
File.WriteString( File.DirApp, "string.txt", sb.ToString)

' Variant 3
Dim tw As TextWriter
tw.Initialize( File.OpenOutput( File.DirApp, "writer.txt", False))
tw.Write( sb.ToString)
tw.Close

' Variant 4
tw.Initialize( File.OpenOutput( File.DirApp, "line.txt", True))
For Each item As Int In li
    tw.WriteLine( item)
Next
tw.Close
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim raf As RandomAccessFile

    Dim li As List
    li.Initialize2( Array As Int( 1,2,3))
    Log($"Size of list before writing: ${li.Size}"$)
   
    raf.Initialize(File.DirInternal, "list.dat", False)
    raf.WriteB4XObject(li,0)
   
    Dim new As List = raf.ReadB4XObject(0)
    Log($"Size of list after reading: ${new.Size}"$)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Size of list before writing: 3
Size of list after reading: 3
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The (non-android) software I use to open the created file checks the number of lines
Sorry, i oversee this information... A non android app is reading the file.
Is it a b4j or b4i app? If yes, then you can use the code above. It is cross platform as far as i know.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
You could write your own write and read function so you will be able to use it in your android and non android app.

What languagr are u using for the non android app.?
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
this variant works 3 lines by 3 items
B4X:
Dim li AsList
li.Initialize2( ArrayAs Int( 1,2,3))

' Variant 2
Dim sb As StringBuilder
sb.Initialize
For Each item As Int In li
        sb.Append( CRLF).Append( item)
Next
If sb.Length > 0 Then sb = sb.Remove( 0,1)
File.WriteString( File.DirApp, "string2.txt", sb.ToString)
 
Upvote 0

andyp

Member
Licensed User
Knoppi - thank you! This did exactly what I required, and I am sure will be of use to others......

Very clever putting the CRLF first, then removing it :)

Here is the code relevant to my original example.

B4X:
        Dim saved As StringBuilder
        saved.Initialize
        saved.Append(CRLF).Append("Item2")
        saved.Append(CRLF).Append("Item1")
        If saved.Length > 0 Then saved = saved.Remove(0,1)
        File.MakeDir(File.DirInternal, "/mydirectory")
        File.WriteString(File.DirInternal&"/mydirectory", "savedfile", saved)
 
Upvote 0
Top