通过TextFieldParser类收集以字节为单位的行

时间:2018-05-13 03:44:03

标签: .net vb.net csv

要快速导航到CSV文件中的某些行,我需要记住它们在给定文件中的位置(以字节为单位)。我正在使用TextFieldParser课程。使用TextFieldParser我想处理文件中的数据,我还想记住某些行的字节偏移量。这可能吗?

我对Position方法返回的数据感到困惑,因为当TextFieldParser.ReadLine方法在文件中前进时,Position方法返回相同的值(5120),直到第62行。

Dim FStream As FileStream = File.Open("p.csv", FileMode.Open)
' My plan is to pass to this methond some offset in bytes from
' beginning of file so when csvReader.ReadLine() is called it reads
' needed line.
'FStream.Seek(5120, SeekOrigin.Begin)
Using csvReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FStream, System.Text.Encoding.GetEncoding(1251))
    csvReader.TextFieldType = FileIO.FieldType.Delimited
    csvReader.SetDelimiters(";")
    csvReader.HasFieldsEnclosedInQuotes = False
    csvReader.TrimWhiteSpace = True

    While csvReader.LineNumber < 100
        Try
            Dim aa = csvReader.ReadLine()
            Console.WriteLine(aa)
            Console.WriteLine("Line number {0}. Position: {1}", csvReader.LineNumber, FStream.Position())
        Catch ex As Exception
        End Try
    End While

    outputFile.Close()
End Using

这是输出:

1
storage_code;article;brand;description;delivery_time;count_text;miltiplity_;price
Line number 2. Position: 5120
Bella333;SLHGO038;NAMDO;"ODL mdiriuo fofofof";0/2;2;1;.59,10
... It prints «Position: 512» until line 62 when it outputs «Position: 9216»...

0 个答案:

没有答案