Introduction
The FileIO.TextFieldParser object provides methods and properties for parsing structured text files. Structured text files are often used to import or merge external data from partners to the main database of a company. With FileIO.TextFieldParser, you can easily read text files with structured content like below:
ID AMOUNT DATE FLAG
000010000025021009AAA1
and get the results into a string array by setting the SetFieldWidths property and the Path of the file you want to read.
FileIO.TextFieldParser.SetFieldWidths(4, 7, 6, 4)
More info about this class can be found on MSDN.
But what about if you want to write instead of read fixed-length data into a file?
The TextFieldWriter class is a very simple class which implements the opposite of FileIO.TextFieldParser to easily write fixed-length fields into a text file.
Using the code
Public Class TextFieldWriter
Public Enum LeadingSides
Left = 0
Right = 1
End Enum
TextFieldWriter supports only fixed-length fields, and you may need to fill the remaining length of a field with a specific character on the left or the right side.
Private _struct As New List(Of Char())
Initialize a new list of Char which will be the "template" of the fixed-length fields. Typically, an instance of the class represents a single line on a text file.
Public Sub SetFieldWidths(ByVal ParamArray fieldWidths() As Integer)
Try
_struct.Clear()
For i As Integer = 0 To fieldWidths.Length - 1
_struct.Add(StrDup(fieldWidths(i), Space(1)))
Next
Catch ex As Exception
Throw ex
End Try
End Sub
Use the SetFieldWidths method to declare through a param array the length of each field.
Public Sub SetFieldValue(ByVal index As Integer, _
ByVal value As String, _
Optional ByVal leadChar As Char = "", _
Optional ByVal leadCharSide As LeadingSides = LeadingSides.Left)
Dim i As Integer = 0
Try
If (leadChar <> String.Empty) Then
If (value.Length < _struct(index).Length) Then
Select Case leadCharSide
Case LeadingSides.Left
value = StrDup((_struct(index).Length - value.Length), leadChar) & value
Case LeadingSides.Right
value = value & StrDup((_struct(index).Length - value.Length), leadChar)
End Select
End If
End If
For Each c As Char In value.ToCharArray
If _struct(index).Length > i Then
_struct(index)(i) = c
i += 1
End If
Next
Catch ex As Exception
Throw ex
End Try
End Sub
Use SetFieldValue to assign a value to a field. If the value is longer than the field's length, then it's trimmed. If not, you have an option to fill the remaining space by using the leadChar and leadCharSide properties.
Public Function GetFieldValue(ByVal index As Integer) As String
Try
Return String.Concat(_struct(index))
Catch ex As Exception
Throw ex
End Try
End Function
Use GetFieldValue to get the value of a field as a string.
Public Overrides Function ToString() As String
Dim values As String = String.Empty
Try
For i As Integer = 0 To _struct.Count - 1
values += String.Concat(_struct(i))
Next
Return values
Catch ex As Exception
Throw ex
End Try
End Function
An overloaded ToString function returns the whole structure as a string.
Example
The example below shows how to write structured text on a text file using TextFieldWriter:
Dim sw As New IO.StringWriter
Dim line As New TextFieldWriter
line.SetFieldWidths(4, 7, 6, 4)
line.SetFieldValue(0, "1", leadChar:="0")
line.SetFieldValue(1, "25", leadChar:="0")
line.SetFieldValue(2, Date.Now.ToString("ddMMyy"))
line.SetFieldValue(3, "1", leadChar:="A")
sw.WriteLine(line.ToString)
line.SetFieldValue(0, "2", leadChar:="0")
line.SetFieldValue(1, "8", leadChar:="0")
line.SetFieldValue(2, Date.Now.ToString("ddMMyy"))
line.SetFieldValue(3, "2", leadChar:="B")
sw.WriteLine(line.ToString)
sw.Flush()
System.IO.File.WriteAllText("myfile.ext", sw.ToString)
sw.Close()