pliroforikos Active Member Licensed User Mar 20, 2021 #1 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. Click to expand... 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. Click to expand... So it creates a new file with same name? And what about appending? Thank you all Attachments Project.zip Project.zip 8.5 KB · Views: 748
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. Click to expand... 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. Click to expand... So it creates a new file with same name? And what about appending? Thank you all
ilan Expert Licensed User Longtime User Mar 20, 2021 #2 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: Mar 21, 2021
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.
pliroforikos Active Member Licensed User Mar 20, 2021 #3 ilan said: 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 safe the data but like this you can append data to data that is already existing. Click to expand... Thank you, something without converting to bytes?
ilan said: 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 safe the data but like this you can append data to data that is already existing. Click to expand... Thank you, something without converting to bytes?
ilan Expert Licensed User Longtime User Mar 20, 2021 #4 pliroforikos said: Thank you, something without converting to bytes? Click to expand... 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 said: Thank you, something without converting to bytes? Click to expand... 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 Mar 20, 2021 #5 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.
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 Mar 20, 2021 #6 pliroforikos said: 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. Click to expand... Good Luck Last edited: Mar 20, 2021
pliroforikos said: 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. Click to expand... Good Luck
Erel B4X founder Staff member Licensed User Longtime User Mar 21, 2021 #7 Another option is to use TextWriter: Code: 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.
Another option is to use TextWriter: Code: 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.