Append to Text file

Hello,
This might be something really simple but I can’t figure out. I want to:

  1. Check to see if a file exists.
  2. If it does, it will open to append data
  3. If it does not, it will create it and append the first record

I did this but I always get “new file” and I always get just the last record in the file.
The code is in a TCPSocket method called “processdata” which is called everytime data comes in.

Var file As FolderItem
file = new FolderItem("Trans.csv")
Var output As TextOutputStream
If file <> Nil Then
  msgbox "new file"
  output = TextOutputStream.Create(file)
  output.Close
Else
  msgbox "existing"
end if
output = TextOutputStream.open(file)
output.WriteLine(sReceived.ToText) + CHR(13) + CHR(10)
output.Close

You probably want to flush the stream before closing, especially if s.Received.ToText is small.

output.flush

The problem is, file will never be nil. Calling new gets a new instance, never nil. If the file doesn’t exist, it merely points to the nonexistent path.

Further, according to the docs, you shouldn’t need to do all this. Simply call Open and, if the file doesn’t exist, it will be created.

1 Like

Thank you guys,
This did the trick

Var file As FolderItem
file = new FolderItem("ScaleTrans.csv")
Var output As TextOutputStream
If file.Exists Then
  output = TextOutputStream.open(file)
Else
  output = TextOutputStream.Create(file)
end if

output.WriteLine(sReceived.ToText)
output.Close

Here is the Method I use:

Protected Sub doStringToFile(fileData As String, f As FolderItem, asBinary As Boolean = False, appendText As Boolean = False)
  'convert a string back into a file
  'CopyFile creates a read only file, whereas this routine creates a file that can be written to
  Var tempTextOutputStream As TextOutputStream
  Var tempBinaryStream As BinaryStream
  
  'check if we are able to create the file
  If f = Nil Then Return 'or f.ChildAt.isAlias
  
  If f.Exists And appendText Then
    If asBinary Then
      tempBinaryStream = BinaryStream.Open(f, True)
      tempBinaryStream.Write(fileData)
      'forum.xojo.com/t/sending-jpg-files-via-urlconnection-fails-sometimes/70104/2
      'tempBinaryStream.Flush 'ensure it is all out of the cache
      tempBinaryStream.Close
      
    Else
      tempTextOutputStream = TextOutputStream.Open(f)
      tempTextOutputStream.Write(fileData)
      'tempTextOutputStream.Flush
      tempTextOutputStream.Close
    End If
    
  Else
    If f.Exists Then
      Try
        f.Remove 'remove any existing copy
      Catch Error
        Break
      End Try
      If f.Exists Then Return 'don't try to write over a file we can't delete!
    End If
    
    If asBinary Then
      tempBinaryStream = BinaryStream.Create(f, True)
      tempBinaryStream.Write(fileData)
      'tempBinaryStream.Flush
      tempBinaryStream.Close
      
    Else
      tempTextOutputStream = TextOutputStream.Create(f)
      tempTextOutputStream.Write(fileData)
      'tempTextOutputStream.Flush
      tempTextOutputStream.Close
    End If
  End If    
End Sub