B4J Question Append String to File does not work [SOLVED]

GMan

Well-Known Member
Licensed User
Longtime User
I have issues when executig this:

B4X:
        Dim myString As String
        File.OpenOutput(File.DirApp,"test.txt",True)
        File.WriteString(File.DirApp,"test.txt",myString)
The output is always only one line at the top, no strings are appended
 

Swissmade

Well-Known Member
Licensed User
Longtime User
I have issues when executig this:

B4X:
        Dim myString As String
        File.OpenOutput(File.DirApp,"test.txt",True)
        File.WriteString(File.DirApp,"test.txt",myString)
The output is always only one line at the top, no strings are appended
Where is the value of myString.
myString is empty in your case here
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Its filled before with the data...the value is written correct, but no appending.
I also tried other Directories like C:\Test, but also the same result
 
Upvote 0

amykonio

Active Member
Licensed User
Longtime User
Its filled before with the data...the value is written correct, but no appending.
I also tried other Directories like C:\Test, but also the same result
Have you checked what File.WriteString is meant to do?

1723631913599.png

Andreas.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Its filled before with the data...the value is written correct, but no appending.
I also tried other Directories like C:\Test, but also the same result
Is it add the value at all?

So as you show the code there is no value in myString
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Maybe better use TextWriter

Textwriter:
Public Sub WriteErrorLog(str As String)
    Try
        Dim tmpTextWriter As TextWriter
        Dim tmpWhereFrom As String
        If Main.IsServer = True Then
            tmpWhereFrom = "-Server-"
        Else
            tmpWhereFrom = "-Client-"
        End If
        DateTime.DateFormat = "dd-MM-yyyy"
        Dim LogDate As String = DateTime.Date(DateTime.Now)
        DateTime.TimeFormat = "HH:mm:ss.SSS"
        If File.Exists(Main.DirApp, "Logs/" & LogDate & "-" & Main.BrandName & tmpWhereFrom & "ErrorLog.txt") = True Then
'            If File.Size(Main.DirApp, "Logs/" & LogDate & "-" & Main.BrandName & tmpWhereFrom & "ErrorLog.txt") > 100000000 Then
'                File.Delete(Main.DirApp, "Logs/" & LogDate & "-" & Main.BrandName & tmpWhereFrom & "ErrorLog.txt")
'            End If
        End If
        tmpTextWriter.Initialize(File.OpenOutput(Main.DirApp, "Logs/" & LogDate & "-" & Main.BrandName & tmpWhereFrom & "ErrorLog.txt", True))
        tmpTextWriter.WriteLine(DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now) & " " & str)
        tmpTextWriter.Close
        DateTime.TimeFormat = "HH:mm:ss"
    Catch
        ShowLog("Error: Write Error Log", False, False, LastException.Message)
    End Try

End Sub
 
Upvote 0

amykonio

Active Member
Licensed User
Longtime User
Or, use file.reastring, concatenate the new value and then use file.writestring. Of course textwriter is better option...
Andreas.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Or, use file.reastring, concatenate the new value and then use file.writestring. Of course textwriter is better option...
Andreas.
Hope you can use it.
Enjoy coding.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
It is working but you are using it the wrong way.

B4X:
Dim inStr As InputStream = File.OpenInput(File.DirApp, "sample.txt")
Dim outStr As OutputStream = File.OpenOutput(File.DirApp, "test.txt", True)
File.Copy2(inStr, outStr)
outStr.Close
 
Upvote 0

emexes

Expert
Licensed User
Lol I am so late to this party, but... I've already written this up, so here it is in case it's of use:

B4X:
Sub FileAppendString(FileDir As String, FileName As String, NewString As String)
    Dim NewBytes() As Byte = NewString.GetBytes("UTF8")   

    Dim fh As OutputStream = File.OpenOutput(FileDir, FileName, True)
    fh.WriteBytes(NewBytes, 0, NewBytes.Length)
    fh.Close
End Sub

Sub FileAppendLine(FileDir As String, FileName As String, NewLine As String)
    FileAppendString(FileDir, FileName, NewLine & CRLF)
End Sub
 
Last edited:
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
i solved it this way - Thx 2 @All
B4X:
Public Sub Schreibpreis(str As String)
    Try
        Dim tmpTextWriter As TextWriter
        DateTime.DateFormat = "dd.MM.yyyy"
        DateTime.TimeFormat = "HH:mm:ss"
        If File.Exists(File.DirApp, "test.txt") = True Then
        End If
        tmpTextWriter.Initialize(File.OpenOutput(File.DirApp, "test.txt", True))
        tmpTextWriter.WriteLine(DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now) & " " & myString)
        tmpTextWriter.Close
    Catch
        Log("Error: Write Error Log " & LastException.Message)
    End Try

End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my sub. Not sure I created myself or copied it from somewhere else.
B4X:
Sub WriteTextOutput (Dir As String, FileName As String, Contents As String)
    Dim Out As OutputStream = File.OpenOutput(Dir, FileName, True)
    Dim b() As Byte = (CRLF & Contents).GetBytes("UTF8")
    Out.WriteBytes(b, 0, b.Length)
    Out.Close
End Sub

B4X:
DateTime.DateFormat = "dd.MM.yyyy HH:mm:ss"
Dim myString As String = DateTime.Date(DateTime.Now) & " - " & "Some log data"
WriteTextOutput(File.DirApp, "test.txt", myString)
 
Last edited:
Upvote 0
Top