iOS Question Not Appending to Text File

cbanks

Active Member
Licensed User
Longtime User
Why doesn't this code append to an existing file? It appears to be overwriting it:

B4X:
    File.OpenOutput(File.DirLibrary, "History.txt", True)
File.WriteString(File.DirLibrary, "History.txt", url)

So, whenever I tell it to display each line from the file, it only displays one line like it is not appending, just overwriting.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no relation between the two commands.

File.OpenOutput returns an OutputStream which you are not using.

File.WriteString writes the string to a file, overwriting any existing file.

You should use:
B4X:
Dim out As OutputStream = File.OpenOutput(...)
Dim t() As Bytes = url.GetBytes("UTF8")
out.WriteBytes(t, 0, t.Length)
out.Close
 
Upvote 0

cbanks

Active Member
Licensed User
Longtime User
Thank you. I had to change Bytes to Byte to get it to work:

B4X:
    Dim out As OutputStream = File.OpenOutput(File.DirLibrary, "History.txt", True)
Dim t() As Byte = url.GetBytes("UTF8")
out.WriteBytes(t, 0, t.Length)
out.Close

Does this code append on a new line each time it is run? I want it to append to a new line.
 
Last edited:
Upvote 0
Top