Explaining text files

pliroforikos

Active Member
Licensed User
Today i'm playing with files but i have a question maybe someone can help.



File.OpenOutput (Dir As String, FileName As String, Append As Boolean)
- Opens the given file for output, the Append parameter tells whether the text will be added at the
end of the existing file or not. If the file doesn't exist it will be created.

Acording File.OpenOutput if i set Append to True i will add new data at the end of the file

but the code bellow deletes the previous data

B4X:
File.OpenOutput(strFolder, fn, True)
    
Private msg As String = "Hello World 2"
File.WriteString(strFolder, fn, msg)
    
Private msg As String = "Hello World 3"
File.WriteString(strFolder, fn, msg)

In booklets i'm reading
File.WriteString (Dir As String, FileName As String, Text As String)
- Writes the given text to a new file.

So it creates a new file with same name? And what about appending?

Thank you all
 

Attachments

  • Project.zip
    8.5 KB · Views: 364

ilan

Expert
Licensed User
Longtime User
File.writestring() will create a new file (if it doesnot already exists) and if it exists it will overwrite the existing file.

if you want to append any data to a file you can use outputstream like this:

B4X:
    Dim txt As String = "test "
    Dim bytes() As Byte = txt.GetBytes("UTF8")
    Dim out As OutputStream = File.OpenOutput(File.DirApp, "test.txt", True)
    out.WriteBytes(bytes,0,bytes.Length)
    out.Close

you will need to convert the data to bytes to save the data but like this you can append data to data that is already existing.
 
Last edited:

pliroforikos

Active Member
Licensed User

Thank you,
something without converting to bytes?
 

ilan

Expert
Licensed User
Longtime User
Thank you,
something without converting to bytes?

maybe something like this:

B4X:
    File.WriteString(File.DirApp,"test.txt","this is the first line")
   
    '....'
   
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(File.ReadString(File.DirApp,"test.txt")).Append(CRLF).Append("this is the second line")
    File.WriteString(File.DirApp,"test.txt",sb.ToString)
 

pliroforikos

Active Member
Licensed User
Thank you again
I'm looking for a solution that it will be easy to explain to young students that they are dealing with files for a first time.
And all time i have to do is about 90 - 130 minutes. In this time i have to explain everything about text files, adding text, list and maps making new folders and do some examples and give them some exercises.
 

ilan

Expert
Licensed User
Longtime User

Good Luck
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another option is to use TextWriter:
B4X:
Dim out As OutputStream = File.OpenOutput(xui.DefaultFolder, "test.txt", True)
Dim tw As TextWriter
tw.Initialize(out)
tw.WriteLine("hello")
tw.WriteLine("good bye")
tw.Close

Note that writing to File.DirApp involves an assumption that it is possible to write to the program folder. In many cases it will not possible as the program will be inside Program Files, which is a restricted folder.

It is safer to use xui.DefaultFolder = File.DirData.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…