### Write List to Textfile

To write the content of a List to a Textfile, make use of the TextWriter object.
Example 1 Write
Dim l As List
l.Initialize
l.Add("A")
l.Add("B")
l.Add("C")
Dim tw As TextWriter
Try
tw.Initialize(File.OpenOutput(File.DirApp, "test.txt", False))
tw.WriteList(l)
tw.Flush
tw.Close
fx.ShowExternalDocument(File.GetUri(File.DirApp, "test.txt"))
Catch
Log(LastException.Message)
If tw.IsInitialized Then tw.Close
End Try
Example 2 Write
' Write the content of a list to a textfile
' Parameter: l = List, d = Folder, f = Textfile
' Returns: True if OK
`Sub writeListToTextFile(l As List, d As String, f As String) As Boolean
Dim Result As Boolean = False
Dim tw As TextWriter
If l.IsInitialized = False Then
Return Result
End If
Try
Dim tw As TextWriter
tw.Initialize(File.OpenOutput(d, f, False))
For i = 0 To l.Size - 1
tw.WriteLine(l.Get(i))
Next
tw.Flush
tw.Close
Result = True
Catch
Log("Write Text File: Error " & LastException.Message)
If tw.IsInitialized Then tw.Close
End Try
Return Result
End Sub`
`Sub buttonTest_Action
Dim l As List
l.Initialize2(Array As Int(1,2,3,4,5))
writeListToTextFile(l, File.DirApp, "a.lst")
End Sub`
Example 3 Read and Write
`Sub ReadFile2List(d As String, f As String) As List
Dim tr As TextReader
Dim l As List
l.Initialize
Try
' init the textreader and read content into the list
tr.Initialize(File.OpenInput(d, f))
l.AddAll(tr.ReadList)
tr.Close
Catch
Log("Read Text File: Error " & LastException.Message)
If tr.IsInitialized Then tr.Close
End Try
Return l
End Sub`
`Sub SaveList2File(l As List, d As String, f As String)
Dim tw As TextWriter
Try
tw.Initialize(File.OpenOutput(d, f, False))
tw.WriteList(l)
tw.Flush
tw.Close
Catch
Log(LastException.Message)
If tw.IsInitialized Then tw.Close
End Try
End Sub`

Changed: , Category: TextReader and TextWriter, File: T6C263M1955.htm, ID: 1955