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.
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.
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.
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)
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.
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.