Android Question TextWriter Not creating a file

rkmoray

Active Member
Licensed User
Longtime User
I am trying to create a logging file(my app is crashing on 1 customers sight), and I
get the error message "java.io.FileNotFoundException: true/newfile.log (No such file or directory)"
when I try to open/write to the file.
Here is my code.
B4X:
Sub LogWrite(EventsLog As String)
      
    If File.ExternalWritable = False Then
        Msgbox("Cannot write log file.", "")
        Return
    Else
        Dim TextWriter1 As TextWriter
        TextWriter1.Initialize(File.OpenOutput(File.ExternalWritable, "newfile.log", True))
        TextWriter1.WriteLine("- " & EventsLog)
        TextWriter1.Close
    
    End If
 
End Sub
 

stevel05

Expert
Licensed User
Longtime User
File.ExternalWritable returns a boolean value (as you know because you are testing it) so the line

B4X:
 TextWriter1.Initialize(File.OpenOutput(File.ExternalWritable, "newfile.log", True))

needs changing to point to a valid directory and not a boolean value.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
get the error message "java.io.FileNotFoundException: true/newfile.log (No such file or directory)"
Hi @rkmoray: Mr. Erel frowns at using TexWriter and TextReader. You should have a code similar to this:
B4X:
Sub LogWrite(EventsLog As String)
    Dim rp As RuntimePermissions   'need runtime permissions lib. This line preferably in Starter service
    If File.ExternalWritable = False Then
        MsgboxAsync("Cannot write log file.", "")
        Return
    Else
        File.WriteString(rp.GetSafeDirDefaultExternal(""), "newfile.log", EventsLog) 
    End If
End Sub
 
Upvote 0
Top