Android Question SaveAs sometimes copies zero bytes

I'm writing a file using TextWriter from my app in DirInternal, then saving it via SaveAs (published code snippet). About half the time it works perfectly, but sometimes the resulting file is zero bytes long.
Is there any flushing I need to do?
How can I see the file in DirInternal? Files (Google app) won't show it.

B4X:
Private Logger As TextWriter
Logger.Initialize(File.OpenOutput(File.DirInternal, "tempfile.csv", False))
Logger.WriteLine(stuff)

Then closing the TextWriter and copying it using SaveAs:

B4X:
        Logger.Close
        
        ' SaveAs the file to someplace sensible (like Downloads)
        ' Date/time code  to generate a unique filename to save
        Dim filename As String
        DateTime.DateFormat = "yyyyMMdd"
        DateTime.TimeFormat = "HHmmss"
        filename = "Output-" & DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now) & ".csv"
        Wait For (SaveAs(File.OpenInput(File.DirInternal, "tempfile.csv"), "application/octet-stream", filename)) Complete (Success As Boolean)
        File.Delete(File.DirInternal, "tempfile.csv")
 
I can't write all the data in one go with File.WriteString, as there may be 1000's of lines accumulating over time.

Files is being saved to Downloads. Problem is intermittent; running it again unchanged often produces a correct output.

Logging the size of the tempfile gave 989 bytes when I tried it just now; the resulting copy in downloads was 0 bytes. The copy is done by File.Copy2 in the SaveAs snippet, so it seems TextWriter is not to blame. Would File.Copy work?
 
Upvote 0
If I do
B4X:
File.OpenOutput(File.DirInternal, "tempfile.csv", False)
and the tempfile.csv already exists, will it create a new file under a different name? If so, I can test for existence and delete first...
Is there any way to determine the filename created, given an existing output stream?
I can't see what's in DirInternal with a file manager- is there any way to do that?
 
Upvote 0
I put a log after the SaveAs to indicate success, and found that when the file is created with size zero, it never comes out of the SaveAs. I'll look into this some more and raise a new question since it's off-topic.
 
Upvote 0
Top