Android Question TextWriter / Android file size limit?

JeffT

Member
Licensed User
Hi all. Ive been creating a text file using TextWriter.
When I examine the result, it has been truncated at 04000 Hex bytes in size.. it's incomplete.
Is this an Android limit, or TextWriter, or what?
 

DonManfred

Expert
Licensed User
Longtime User
Probably free memory
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is this an Android limit, or TextWriter, or what?
Third option. I guess that you logged the file content. Log truncates messages longer than 4000 characters. TextWriter or File.WriteString will never truncate anything. If there isn't enough memory (and there is) then there will be an error.

Tip: don't use TextWriter. Use File.WriteString or WriteList.
 
Upvote 0

JeffT

Member
Licensed User
Hmm.
Wasnt using logging.. just a loop with TextWriter.
I did want to process each item in my list with a replace() as they were written out.
To make a single writable string for WriteString, Id have to join a lot of strings, and that takes a very long time when debugging. (not on release, but I do need to debug)

I'll see what happens with Writelist
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Id have to join a lot of strings, and that takes a very long time when debugging.
How are you joining your strings?
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
Tip: don't use TextWriter. Use File.WriteString or WriteList.

I'm also fighting with TextWriter during my attempts to port code to multiple platforms, since TextWriter is not available in B4i.
As I'm writing one line a time as they are produced (actually a logging module), File.WriteString or File.WriteList are not suitable, since they write all the file at once, with no append option available.
Or am I missing something?
 
Upvote 0

JeffT

Member
Licensed User
I was using s = s & <newvalue>
I've switched to Writelist and that seems to work (instead of writing line by line, I now add to a list then dump the lot) , but I suspect a bug in TextWriter.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
I was using s = s & <newvalue>
I've switched to Writelist and that seems to work (instead of writing line by line, I now add to a list then dump the lot) , but I suspect a bug in TextWriter.

looks like a waste of memory and processing time, building a new string every time and rewriting the file.
if something bad happens in between, you’re losing the whole file, which defies the purpose of a progressive log, in my use case
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
If you are writing a logs file then open it with File.OpenOutput with Append = True. Write the string. You can do it with OutputStream.Write.

that’s what I’m doing, replacing the TextWriter class with a TextLogger one.
just wondering why this isn’t available cross platform...
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
Everything needed is available:
B4X:
Public Sub LogToFile (msg As String)
    Dim out As OutputStream = File.OpenOutput(xui.DefaultFolder, "log.txt", True)
    'you might want to add: msg = msg & CRLF
    Dim b() As Byte = msg.GetBytes("UTF8")
    out.WriteBytes(b, 0, b.Length)
    out.Close
End Sub

no doubt it can be done, I was just surprised to not find such a basic class not available on all platforms.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
I am using TextWriter in one of my B4J based projects. The files are created as soon as I get MQTT message. Can it be the reason , that after a month or so of logging, I get GC limit error? I had raised this question and Erel suggested me to check the dump file, but it was not possible due to client location.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Maybe this tool can help you while debugging.
 
Upvote 0
Top